¿Cómo pasar una matriz de parámetros a bind_param?

i'm getting the folowing error:

Warning: Wrong parameter count for mysqli_stmt::bind_param()

i believe it's because i'm assigning the parameters to the bind_param method the wrong way. The question is how to pass an array of parameters to the bind_param?

Esta es mi función:

function read($sql, $params)
   $parameters = array();
   $results = array();

   // connect
   $mysql = new mysqli('localhost', 'root', 'root', 'db') or die('There was a problem connecting to the database. Please try again later.');

   // prepare
   $stmt = $mysql->prepare($sql) or die('Error preparing the query');

   // bind param ????????????
   call_user_func_array(array($stmt, 'bind_param'), $params);

   // execute

   // bind result
   $meta = $stmt->result_metadata();

   while ( $field = $meta->fetch_field() ) {
      $parameters[] = &$row[$field->name]; 

   call_user_func_array(array($stmt, 'bind_result'), $parameters);

   // fetch
   while( $stmt->fetch() ) {
     $x = array();

     foreach( $row as $key => $val ) {
        $x[$key] = $val;

     $results[] = $x;

  return $results;

This is how i call it:

$params = array('i'=>$get_release, 'i'=>$status);
$result_set = read('SELECT release_id, release_group_id, status_id, name, subname, description, released_day, released_month, released_year, country_id, note, is_master_release, seo_url, meta_description, is_purchased FROM `release` WHERE release_id = ? AND status_id = ?', $params);

preguntado el 04 de septiembre de 13 a las 02:09

Are you trying to execute multiple query or try to bind many parameters to a single query? -

im trying to bind many parameters to a single query. I though of using an associative array to pass the type and the value to bind_param -

Any reason you're using MySQLi? I would think that PDO's discrete PDOStatement::bindParam encajaría mejor -

@Phil maybe for a future update i'll use pdo -

1 Respuestas

Esto se ve mal:

$params = array('i'=>$get_release, 'i'=>$status);

De los docs:

Tenga en cuenta que cuando se definen dos índices idénticos, el último sobrescribe al primero.

You're passing an array with one element and your statement has two placeholders.

Respondido el 04 de Septiembre de 13 a las 02:09

the array has 2 elements (placeholders), no? the type i for $get_release and type i for $status - Marco

@Marco - The syntax for array() is that the index (an integer or a string) goes to the left of => and the value to the right. You're using the same index for both $get_release y $status. The second one clobbers the first, so your array is only one long. According to los docs, for two placeholder positions, you should be passing three arguments to bind_param, so the array should be: array('ii', $get_release, $status). - ted salto

i didnt know you could pass all in one array like that - Marco

im not sure what to do next inside my function, once i declare the array that way - Marco

@Marco - See este hilo for a solution. It's very close to what you're trying to do. - ted salto

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.