【问题标题】:query an array using mysqli prepare使用 mysqli prepare 查询数组
【发布时间】:2018-08-11 06:42:15
【问题描述】:

我有一个大型数据库,我获取了客户端 ID,并尝试将这些 ID 用于下一个查询。 我尝试只查询 1 次,而不是尝试查询超过 1000 次。

这是我的查询:

$query_inv_packs_for_date_range = "SELECT invid, packid, clientid, 
date_range_start, date_range_end, value FROM inv_packs WHERE clientid 
IN(implode(',', $in))";

然后我尝试通过解包函数参数中的数组来绑定参数。

if($stmt = mysqli_prepare($connection, $query_inv_packs_for_date_range)){

             mysqli_stmt_bind_param($stmt, $types, ...$uniqueIds);
             mysqli_stmt_execute($stmt);
             mysqli_stmt_bind_result($stmt, $invoice_id, $pack_id, $client_id_from_inv_pack_table, $date_range_start, $date_range_end, $value);

             while(mysqli_stmt_fetch($stmt)){
                $invoice_packs_table_data[$inv_pack_count]['invoiceID'] = $invoice_id;
                $invoice_packs_table_data[$inv_pack_count]['packID'] = $pack_id;
                $invoice_packs_table_data[$inv_pack_count]['clientID'] = $client_id_from_inv_pack_table;
                $invoice_packs_table_data[$inv_pack_count]['date_range_start'] = $date_range_start;
                $invoice_packs_table_data[$inv_pack_count]['date_range_end'] = $date_range_end;
                $invoice_packs_table_data[$inv_pack_count]['value'] = $value;
                $inv_pack_count++;
                // get date ranges, and create variables that are going to be outputted immediately at the end of all calculations
                // maybe not... just put them all in the array
             }
             $stmt->close();
         }

我怎样才能让这个查询工作。

$in 

$in = 问号数组,效果很好,$uniqueids 有超过 1,000 个代表 ID 的唯一数字。

【问题讨论】:

  • 我不认为它是重复的,我想弄清楚如何在我的查询调用中使用 implode 函数,然后使用 '...' 绑定参数
  • 如果不是重复的,它可能会帮助您简化“动态绑定”情况,因为您不可能在mysqli_stmt_bind_param 函数调用中输入一千个不同的变量名,是吗?使用 call_user_func_array 会有所帮助。

标签: php mysql arrays in-clause


【解决方案1】:

...“效果很好”...我们确定它可以正常工作吗?

评估此语句后$query_packs_for_date_range 的外观如何?

$query_inv_packs_for_date_range = "SELECT invid, packid, clientid, 
date_range_start, date_range_end, value FROM inv_packs WHERE clientid 
IN(implode(',', $in))";

为了调试,我会回显或var_dump() $query_inv_packs_for_date_range 的内容,然后再传递给准备。

我认为 $in 正在接受评估,但 implode 函数没有。

就我个人而言,我会这样写:

$query_inv_packs_for_date_range = 'SELECT invid, packid, clientid, 
date_range_start, date_range_end, value FROM inv_packs WHERE clientid 
IN (' . implode(',', $in) . ')';
//   ^^^                 ^^^

--

问号(绑定占位符)的数量需要与绑定值的数量相匹配。

【讨论】:

  • 这绝对帮助了我的查询字符串。我已经在使用点来添加函数,但是我在单引号和双引号之间切换。谢谢您的帮助。我会将您的答案标记为正确。
猜你喜欢
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 2011-02-08
  • 2011-06-29
相关资源
最近更新 更多