Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use multiple WHERE statements when using IN with array
    primarykey
    data
    text
    <p>I have the following code:</p> <pre><code>function av_connections_search($string){ global $wpdb; // remove url parameter from string $string = trim($string,'s='); $search = explode('%20',$string); // query the wp_connections database table according to search entry $sql = " SELECT * FROM {$wpdb-&gt;prefix}connections WHERE contact_first_name IN (".implode(', ', array_fill(0, count($search), '%s')).") "; // prepare() to prevent sql injection $query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $search)); // get query results //var_dump($query); $results = $wpdb-&gt;get_results($query); // return if no results if(empty($results)){ return false; } // flush $wpdb cache $wpdb-&gt;flush(); // return data to search.php return $results; } </code></pre> <p>where <code>$string</code> looks like ?s=search+these+terms when passed to the function</p> <p>My question, how can I use multiple WHERE statements? I've tried simply:</p> <pre><code>WHERE contact_first_name IN (".implode(', ', array_fill(0, count($search), '%s')).") OR contact_last_name IN (".implode(', ', array_fill(0, count($search), '%s')).") </code></pre> <p>but it fails completely. When I do:</p> <pre><code>WHERE contact_first_name OR contact_last_name IN (".implode(', ', array_fill(0, count($search), '%s')).") </code></pre> <p>it only returns <code>contact_last_name</code>. What am I missing?</p> <p><strong>EDIT:</strong> Ok, so I'm quite sure the problem lies within this:</p> <p><code>$query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $search));</code></p> <p>but, for a lack of sleep I cannot wrap my mind around why it's not merging the array with both WHERE clauses.</p> <p><strong>EDIT 2</strong> Should this not work? It works great if I use the single WHERE clause, but the moment I use OR and another clause, it returns nothing, which makes no sense because this is the query:</p> <p><code>SELECT * FROM wp_connections WHERE</code>contact_first_name<code>IN (%s, %s) OR</code>contact_last_name<code>IN (%s, %s) " [1]=&gt; string(4) "Mina" [2]=&gt; string(5) "Morse"</code></p> <p><strong>EDIT 3</strong> I do not believe prepare() is the problem. Consider this (doesn't work):</p> <pre><code>global $wpdb; $string = trim($string,'s='); $search = explode('%20',$string); $how_many = count($search); $placeholders = array_fill(0, $how_many, '%s'); $format = implode(', ', $placeholders); $query = "SELECT * FROM wp_connections WHERE contact_first_name IN($format) OR contact_last_name IN($format)"; $results = $wpdb-&gt;query($query, $search); return $results; </code></pre> <p>Even removing prepare completely, same result. What am I missing? If I remove the <code>OR</code> condition and just check one value, it works fine, but OR kills the query.</p> <p><strong>EDIT 4</strong> It turns out this was the solution:</p> <pre><code>$results = $wpdb-&gt;query($query, $search, $search); </code></pre> <p>I was missing the second $search variable... *scratching head still</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload