Error:Issue when trying to use IN() in wordpress database

0 votes

Hii,

I have this:

$villes = '"paris","fes","rabat"';
$sql    = 'SELECT distinct telecopie FROM `comptage_fax` WHERE `ville` IN(%s)';
$query  = $wpdb->prepare($sql, $villes);

when I do an echo $query; i get:

SELECT distinct telecopie FROM `comptage_fax` WHERE `ville` IN('\"CHAPELLE VIVIERS \",\"LE MANS \",\"QUEND\"')

the probleme i have is that $wpdb add ' in IN('...')

can someone help, thanks

May 8 in PHP by kartik
• 27,220 points
67 views

1 answer to this question.

0 votes

Hello @kartik,

Try this code :

// Create an array of the values to use in the list
$villes = array("paris", "fes", "rabat");    

// Generate the SQL statement.
// The number of %s items is based on the length of the $villes array
$sql = "
  SELECT DISTINCT telecopie
  FROM `comptage_fax`
  WHERE `ville` IN(".implode(', ', array_fill(0, count($villes), '%s')).")
";

// Call $wpdb->prepare passing the values of the array as separate arguments
$query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $villes));

echo $query;

Hope this work!!

thank you!!

answered May 8 by Niroj
• 58,620 points

Related Questions In PHP

0 votes
1 answer

I am trying to run following command But I end up with an error :

Hello Nishant, You are getting this error because your ...READ MORE

answered Apr 9 in PHP by Niroj
• 58,620 points
72 views
0 votes
1 answer

How to get the current plugin directory in WordPress?

Hello @kartik, This will actually get the result ...READ MORE

answered May 8 in PHP by Niroj
• 58,620 points
165 views
0 votes
1 answer

How to load Javascript in Wordpress Plugin?

Hello kartik, You need to specify when the ...READ MORE

answered May 8 in PHP by Niroj
• 58,620 points
64 views
0 votes
1 answer

How to do error logging in CodeIgniter ?

Hello @kartik, CodeIgniter has some error logging functions ...READ MORE

answered May 19 in PHP by Niroj
• 58,620 points
155 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 58,620 points
195 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5 in Java-Script by Niroj
• 58,620 points
321 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8 in Node-js by Niroj
• 58,620 points
61 views
0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8 in Node-js by Niroj
• 58,620 points
196 views
0 votes
1 answer

Error:Jquery - Uncaught TypeError: Cannot use 'in' operator to search for '324' in

Hello @kartik, You have a JSON string, not ...READ MORE

answered Jun 16 in PHP by Niroj
• 58,620 points
481 views
0 votes
1 answer

Error:Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF.

Hello @kartik, You're inserting values for OperationId that is an identity ...READ MORE

answered Jul 21 in PHP by Niroj
• 58,620 points
302 views