0

Hello im new to WPDB and this is my error

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''questions' ORDER by id DESC' at line 1]

SELECT * from 'questions' ORDER by id DESC

This is my code

//Get data from Table
global $wpdb;
$vragen = $wpdb->get_results(
    $wpdb->prepare( "SELECT * from %s ORDER by id DESC", 'questions'), 
    ARRAY_A
);

please halp

1
  • 1
    Does the error go away if you change ORDER by to ORDER BY? Commented Jan 16, 2023 at 12:43

1 Answer 1

1

The problem here you are passing table name as a placeholder in prepare statement. Which is not allowed. You can directly add the table name in your query without quotes. Your query do not have any placeholder and arguments. So No need for prepare statement.

Try following code.

global $wpdb;
$vragen = $wpdb->get_results( "SELECT * from questions ORDER BY id DESC", 
    ARRAY_A
); 
2
  • it works thanks Commented Jan 17, 2023 at 12:48
  • @ShadyMorsi . If this answers your question, please consider marking it as the correct answer. It will helps others find it as resolved.
    – Irfan
    Commented Jan 17, 2023 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.