gpt4 book ai didi

php - 使用 mysqli prepare 查询数组

转载 作者:行者123 更新时间:2023-11-29 07:35:25 25 4
gpt4 key购买 nike

我有一个大型数据库,我获取了客户端 ID,并尝试将这些 ID 用于下一个查询。
我试图只查询 1 次,而不是尝试查询超过 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))";

然后我尝试通过解包函数参数中的数组来绑定(bind)参数,就像这样。
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 的唯一数字。

最佳答案

...“效果很好”...我们确定它可以正常工作吗?
$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) . ')';
// ^^^ ^^^

--

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

关于php - 使用 mysqli prepare 查询数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49079476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com