dtjbcda841554 2018-03-03 00:44 采纳率: 0%
浏览 80
已采纳

使用mysqli prepare查询数组

I have a large database, I grabbed client id's and im trying to use those id's for the next query. I am trying to only query 1 time, instead of trying to query over 1,000 times.

Here is my query:

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

Then I try to bind the parameters by unpacking the array inside the function parameter like so.

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

How can I get this query to work.

$in 

$in = array of question marks, which works fine, and $uniqueids has a little over 1,000 unique numbers representing the ID's.

  • 写回答

1条回答 默认 最新

  • dtwkt46424 2018-03-03 00:53
    关注

    ... "which works fine" ... Are we sure that it works fine?

    what does $query_packs_for_date_range look like after this statement is evaluated?

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

    For debugging, I would echo or var_dump() the contents of $query_inv_packs_for_date_range before I passed it to prepare.

    I'm thinking $in is getting evaluated, but the implode function is not.

    Personally, I'd write it like this:

    $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) . ')';
    //   ^^^                 ^^^
    

    --

    The number of question marks (bind placeholders) will need to match the number of bind values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮