Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't bind an array to an SQL parameter. You can use a parameter in SQL in place of a <em>single literal value</em>. Not a list of values, or an expression, or a column name or table name.</p> <p>To solve the task in your case, you can use either of two solutions:</p> <p>First solution: loop over <code>$genre</code> array, bind each value one at a time, and execute the SQL query for each value.</p> <pre><code>if ($stmt-&gt;prepare($selectGenre_sql)) { $genre = array(); foreach ($gengre as $genreID) { $stmt-&gt;bind_param('s', $genreID); $stmt-&gt;execute(); $stmt-&gt;bind_result($genres); while ($stmt-&gt;fetch()) { $genre[] = $genres; } } } </code></pre> <p>Second solution: execute the query once, with multiple parameters, one for each value in the array. This requires some tricky code to build a variable number of <code>?</code> placeholders in the SQL query, separated by commas.</p> <pre><code>$selectGenre_sql = 'SELECT genreID FROM genres WHERE dbGenre IN (' . join(',', array_fill(0, count($genre), '?')) . ')'; if ($stmt-&gt;prepare($selectGenre_sql)) { $genre = array(); . . . </code></pre> <p>Also you need to get tricky calling <code>bind_param()</code> with a variable number of arguments based on the elements in your <code>$genre</code> array:</p> <pre><code> . . . call_user_func_array( array($stmt, 'bind_param'), array_unshift($genre, str_repeat('i', count($genre))); $stmt-&gt;execute(); $stmt-&gt;bind_result($genres); while ($stmt-&gt;fetch()) { $genre[] = $genres; } } </code></pre> <p>You might want to consider using <code>PDO::mysql</code> because it's easier to bind parameters from an array. The MySQLi interface is pretty awkward for this case.</p>
 

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