duanran3115 2013-09-04 01:12
浏览 97

如何将参数数组传递给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);
  • 写回答

1条回答 默认 最新

  • duanke9540 2013-09-04 01:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭