Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

danixland on "$wpdb->prepare uses single quotes instead of backticks"

$
0
0

Hello everybody,
I'm writing a couple functions that should go and read data from a custom table I've set in the database, right now I'm just using $wpdb->get_var and $wpdb->get_row, both with $wpdb->preare to sanitize my code, but WP is telling me that I have an error in my mysql code, here's a small example that is causing me trouble:

global $wpdb;
$table_name = $wpdb->prefix . "tdg_market";
$total_customers = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %s", $table_name ) );

And here's what this code is returning:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''wp_tdg_market'' at line 1]
SELECT COUNT(*) FROM 'wp_tdg_market'

As you can see the table name is between single quotes as reported from the WordPress error. So I've tried this line:
$total_customers = $wpdb->get_var( "SELECT COUNT(*) FROM wp_tdg_market" )
without using $wpdb->prepare and hard coding the name of the table, and it worked like a charm, so I came to the conclusion that is $wpdb->prepare's fault for using single quotes instead of backticks..

Is there a way to make $wpdb->prepare use backticks instead of single quotes??

Thanks a lot in advance


Viewing all articles
Browse latest Browse all 8245

Trending Articles