• <tfoot id='JEbR6'></tfoot>
      <bdo id='JEbR6'></bdo><ul id='JEbR6'></ul>
    <i id='JEbR6'><tr id='JEbR6'><dt id='JEbR6'><q id='JEbR6'><span id='JEbR6'><b id='JEbR6'><form id='JEbR6'><ins id='JEbR6'></ins><ul id='JEbR6'></ul><sub id='JEbR6'></sub></form><legend id='JEbR6'></legend><bdo id='JEbR6'><pre id='JEbR6'><center id='JEbR6'></center></pre></bdo></b><th id='JEbR6'></th></span></q></dt></tr></i><div id='JEbR6'><tfoot id='JEbR6'></tfoot><dl id='JEbR6'><fieldset id='JEbR6'></fieldset></dl></div>

      <small id='JEbR6'></small><noframes id='JEbR6'>

      <legend id='JEbR6'><style id='JEbR6'><dir id='JEbR6'><q id='JEbR6'></q></dir></style></legend>

      1. MySQLi 准备语句与 IN 运算符

        时间:2023-07-31
      2. <i id='air9a'><tr id='air9a'><dt id='air9a'><q id='air9a'><span id='air9a'><b id='air9a'><form id='air9a'><ins id='air9a'></ins><ul id='air9a'></ul><sub id='air9a'></sub></form><legend id='air9a'></legend><bdo id='air9a'><pre id='air9a'><center id='air9a'></center></pre></bdo></b><th id='air9a'></th></span></q></dt></tr></i><div id='air9a'><tfoot id='air9a'></tfoot><dl id='air9a'><fieldset id='air9a'></fieldset></dl></div>
            <tbody id='air9a'></tbody>
          • <legend id='air9a'><style id='air9a'><dir id='air9a'><q id='air9a'></q></dir></style></legend>

            <small id='air9a'></small><noframes id='air9a'>

                <bdo id='air9a'></bdo><ul id='air9a'></ul>
              • <tfoot id='air9a'></tfoot>

                  本文介绍了MySQLi 准备语句与 IN 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我必须使用 IN 运算符从数据库中选择一些行.我想使用准备好的语句来做到这一点.这是我的代码:

                  I have to select some rows from the database using IN operator. I want to do it using prepared statement. This is my code:

                  <?php
                  $lastnames = array('braun', 'piorkowski', 'mason', 'nash');
                  $in_statement = '"' . implode('", "', $lastnames) . '"'; //"braun", "piorkowski", "mason", "nash"
                  
                  $data_res = $_DB->prepare('SELECT `id`, `name`, `age` FROM `users` WHERE `lastname` IN (?)');
                  $data_res->bind_param('s', $in_statement);
                  $data_res->execute();
                  $result = $data_res->get_result();
                  while ($data = $result->fetch_array(MYSQLI_ASSOC)) {
                      ...
                  }
                  ?>
                  

                  虽然数据库中所有数据都存在,但是什么都不返回.

                  还有一点:如果我直接通过$in_statement来查询执行,会返回数据.所以问题出现在准备阶段.

                  我在谷歌中寻找问题,但没有成功.我的代码有什么问题?
                  感谢您的帮助!

                  But returns nothing although all data exists in the database.

                  And one more: if i pass $in_statement directly to query and execute it, the data will be returned. So the problem appears on preparing.

                  I was looking for the question in Google but it wasn't' successful. What's wrong with my code?
                  Thanks for the help!

                  推荐答案

                  我最近为我的问题找到了解决方案.也许这不是最好的方法,但它很好用!证明我错了:)

                  I've recently found the solution for my question. Maybe it's not the best way to do it, but it works nice! Prove me wrong:)

                  <?php
                  $lastnames = array('braun', 'piorkowski', 'mason', 'nash');
                  $arParams = array();
                  
                  foreach($lastnames as $key => $value) //recreate an array with parameters explicitly passing every parameter by reference
                      $arParams[] = &$lastnames[$key];
                  
                  $count_params = count($arParams);
                  
                  $int = str_repeat('i',$count_params); //add type for each variable (i,d,s,b); you can also determine type of the variable automatically (is_int, is_float, is_string) in loop, but i don't need it
                  array_unshift($arParams,$int); 
                  
                  $q = array_fill(0,$count_params,'?'); //form string of question marks for statement
                  $params = implode(',',$q);
                  
                  $data_res = $_DB->prepare('SELECT `id`, `name`, `age` FROM `users` WHERE `lastname` IN ('.$params.')');
                  call_user_func_array(array($data_res, 'bind_param'), $arParams);
                  $data_res->execute();
                  $result = $data_res->get_result();
                  while ($data = $result->fetch_array(MYSQLI_ASSOC)) {
                      ...
                  }
                  
                  $result->free();
                  $data_res->close();
                  ?>
                  

                  这篇关于MySQLi 准备语句与 IN 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:mySQLi 准备好的语句无法 get_result() 下一篇:mysqli_query(): 无法获取 mysqli 错误

                  相关文章

                  最新文章

                  <tfoot id='F8N1I'></tfoot>

                    <i id='F8N1I'><tr id='F8N1I'><dt id='F8N1I'><q id='F8N1I'><span id='F8N1I'><b id='F8N1I'><form id='F8N1I'><ins id='F8N1I'></ins><ul id='F8N1I'></ul><sub id='F8N1I'></sub></form><legend id='F8N1I'></legend><bdo id='F8N1I'><pre id='F8N1I'><center id='F8N1I'></center></pre></bdo></b><th id='F8N1I'></th></span></q></dt></tr></i><div id='F8N1I'><tfoot id='F8N1I'></tfoot><dl id='F8N1I'><fieldset id='F8N1I'></fieldset></dl></div>
                  1. <legend id='F8N1I'><style id='F8N1I'><dir id='F8N1I'><q id='F8N1I'></q></dir></style></legend>

                    <small id='F8N1I'></small><noframes id='F8N1I'>

                      <bdo id='F8N1I'></bdo><ul id='F8N1I'></ul>