problem in using wpdb->prepare and a string placeholder

I have a pb using wpdb->prepare and string placeholders.

If I do:

$sql = $wpdb->prepare(“SELECT * FROM %s AS T1 LEFT JOIN $table_users AS T2 ON T1.id_assure = T2.ID WHERE T1.id = %d”,$table_dossiers,$id);

then the generated query is:

SELECT * FROM ‘drk4_2_dracard_dossiers’ AS T1 LEFT JOIN drk4_users AS T2 ON T1.id_assure = T2.ID WHERE T1.id = 33

And the query fails because of the quotes around the first table name provided by placeholder: ‘drk4_2_dracard_dossiers’

conversely, if I write both table names in the query (without using a placeholder), it works:

$sql = $wpdb->prepare(“SELECT * FROM $table_dossiers AS T1 LEFT JOIN $table_users AS T2 ON T1.id_assure = T2.ID WHERE T1.id = %d”,$id);

The generated query is:

SELECT * FROM drk4_2_dracard_dossiers AS T1 LEFT JOIN drk4_users AS T2 ON T1.id_assure = T2.ID WHERE T1.id = 33

And it works fine…Do you know what I may do to correct the first code and still use the placeholders for both tables ? Thank you

Read more here:: prepare and a string placeholder” rel=nofollow>problem in using wpdb->prepare and a string placeholder

Leave a Reply

Your email address will not be published. Required fields are marked *