Marco
Marco

Reputation: 2737

How to pass an array of parameters to the 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?

This is my function:

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
   $stmt->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);

Upvotes: 0

Views: 990

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234795

This looks wrong:

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

From the docs:

Note that when two identical index are defined, the last overwrite the first.

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

Upvotes: 1

Related Questions