如何将参数数组传递给bind_param?

时间:2013-09-04 01:12:27

标签: php mysql prepared-statement

我收到了以下错误:

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

我相信这是因为我以错误的方式将参数分配给bind_param方法。问题是如何将一组参数传递给bind_param?

这是我的功能:

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;
}

这就是我所说的:

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

1 个答案:

答案 0 :(得分:1)

这看起来不对:

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

来自the docs

  

请注意,当定义两个相同的索引时,最后一个覆盖第一个索引。

您传递的数组包含一个元素,并且您的语句有两个占位符。