2

I'm trying to display content from SQL tables, using $wpdb object. Problem is, whatever I try using it in my query, it prints a SQL error. What I tried to do is a little more complex than what I'll show you, but I progressively simplified my query to see where the error could come from, but never found.

So here's what I tried last:

<?php

global $wpdb;
$wpdb->show_errors();
$tableCustom = 'join_users_defis';

$requeteAffichage = $wpdb->get_results('
SELECT * 
FROM $wpdb->postmeta
');

var_dump($requeteAffichage);

?>

To me, it's supposed to dump the content of the table {$table_prefix}_postmeta (I tried with others tables), which is a standard WordPress table. But instead, it outputs the following error (thanks to $wpdb->show_errors();):

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 ->_posts at line 2]

My current installation runs locally (XAMPP), with MariaDB 10.1.16 / Apache / PHP 5.6.24

To be sure, I tried querying directly with my WordPress installation prefix, and it worked.

Is it an issue with my installation, or something I missed ? I could use some help on this.

  • 5
    $wpdb->postmeta in single quotes is not read as a variable, just as a string. And that table doesn't exist, of course. – fuxia Mar 21 '17 at 16:49
  • Hi, thank you for your answer. Seems like it's a quote issue. That being said, the table prefix_postmeta exists in the WordPress database ;) – Fl-0 Mar 21 '17 at 18:13
  • 1
    Read this to know the difference between string representations in PHP. It has explanation about single quote vs. double quote. Hint: PHP doesn't replace value of a variable when it is within single quote. So database gets the variable name, not the actual database name inside that variable $wpdb->postmeta – Fayaz Mar 22 '17 at 2:33
3

its a syntax error according to error message. starements like below might work for you.

global $wpdb;
$wpdb->show_errors();
$tableCustom = 'join_users_defis';

$sql = "SELECT * FROM {$wpdb->postmeta}";
$requeteAffichage = $wpdb->get_row( $sql );

//or $requeteAffichage = $wpdb->get_results( $sql );

var_dump($requeteAffichage);
  • Hi, thank you ! It works perfectly. Is there any explanation that the single quotes output an error when double quotes are fine ? – Fl-0 Mar 21 '17 at 18:15
  • actually you placed the variable/object inside single quote which will not print the value of that variable. $some = 'some'; echo '$some'; is wrong. variable cannot be inside quotation marks. you have to concatenate variable with other statement if you want to use single quote $sql = 'SELECT * FROM'.$wpdb->postmeta}; this will work too. – Anwer AR Mar 21 '17 at 18:46
1

You need to use double quoted string literals for PHP to properly interpret variables in the string. Change:

$wpdb->get_results('SELECT * FROM $wpdb->postmeta');

to:

$wpdb->get_results("SELECT * FROM {$wpdb->postmeta}");
1

$wpdb-> doesn't belong in the query.

Change

$requeteAffichage = $wpdb->get_results("
SELECT * 
FROM $wpdb->postmeta
");

to

$prefix = $wpdb->prefix;
$postmeta = $prefix . 'postmeta';
$requeteAffichage = $wpdb->get_results("
SELECT * 
FROM $postmeta
");

(or, if you are using a different table prefix, substitute that prefix for wp_.)

  • Hi ! Thank you for answering. In fact I try to make the query usable for other installations, so I need the prefix to be automatically found. But yes, that way it works ! – Fl-0 Mar 21 '17 at 18:14
  • Updated above to work with any prefix (which also makes it compatible with MultiSite). – WebElaine Mar 21 '17 at 18:48
  • Actually using $wpdb->postmeta is a good thing, it covers all possible table prefix set by the site owner. The main problem was single quoted string. Your updated CODE will not work either as $postmeta variable will be presented to DB directly (with $ sign), not its value :) – Fayaz Mar 22 '17 at 2:40
  • Good catch! Updated with double quotes above so it would work. – WebElaine Mar 23 '17 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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