Using $wpdb generates DB error

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.

3 Answers
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);

Leave a Comment