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
• 11,890 points
27 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
• 23,950 points

Related Questions In PHP

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
• 23,950 points
34 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
• 23,950 points
24 views
0 votes
1 answer

Error: Global Variable is not accessable to local function

Hey kartik, A variable declared outside a function has a ...READ MORE

answered Feb 19 in PHP by Niroj
• 23,950 points
36 views
0 votes
1 answer

What is Php Mysql Database? How php connect to database?

Hello kartik, MySQL is the most popular database ...READ MORE

answered Feb 25 in PHP by Dey
93 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
• 23,950 points
21 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
• 23,950 points
48 views
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17 in Web Development by anonymous
• 11,890 points
48 views
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
• 23,950 points
31 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
• 23,950 points
28 views