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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?