使用mysqli prepare查询数组

问题描述 投票:1回答:1

我有一个大型数据库,我抓住了客户端ID,我正在尝试将这些ID用于下一个查询。我试图只查询一次,而不是尝试查询超过1,000次。

这是我的查询:

$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。

php mysql arrays in-clause
1个回答
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) . ')';
//   ^^^                 ^^^

--

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

© www.soinside.com 2019 - 2024. All rights reserved.