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

        • <bdo id='JKAlt'></bdo><ul id='JKAlt'></ul>
      2. <small id='JKAlt'></small><noframes id='JKAlt'>

        如何使用 mysqli 编写一个安全的 SELECT 查询,该查

        时间:2023-07-30
          <bdo id='9gjox'></bdo><ul id='9gjox'></ul>

                  <small id='9gjox'></small><noframes id='9gjox'>

                  <legend id='9gjox'><style id='9gjox'><dir id='9gjox'><q id='9gjox'></q></dir></style></legend>
                    <tbody id='9gjox'></tbody>
                  <tfoot id='9gjox'></tfoot>
                • <i id='9gjox'><tr id='9gjox'><dt id='9gjox'><q id='9gjox'><span id='9gjox'><b id='9gjox'><form id='9gjox'><ins id='9gjox'></ins><ul id='9gjox'></ul><sub id='9gjox'></sub></form><legend id='9gjox'></legend><bdo id='9gjox'><pre id='9gjox'><center id='9gjox'></center></pre></bdo></b><th id='9gjox'></th></span></q></dt></tr></i><div id='9gjox'><tfoot id='9gjox'></tfoot><dl id='9gjox'><fieldset id='9gjox'></fieldset></dl></div>
                  本文介绍了如何使用 mysqli 编写一个安全的 SELECT 查询,该查询具有可变数量的用户提供的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要一些帮助,以便在我的代码中执行 foreach.

                  I would like some help to execute a foreach into my code.

                  我将多个值发布到用户名中:作为 username[0] = user1 , user2

                  I post multiple value into username : as username[0] = user1 , user2

                  但是我的 foreach 给我的结果只是最后一个条目或什么都没有.

                  but my foreach gives me result like only the last entry or nothing.

                  $companyname = $_POST['companyname'];
                  $username_grab = $_POST['username'];
                  $username = implode(",", $username_grab);
                  
                  foreach ($username_grab as $value){
                      $value = $username_grab;
                  
                      $sql = "select * from linked_user where username = '$value' and company_name = '$companyname'";
                      $res = mysqli_query($conn,$value);
                      while($row = mysqli_fetch_array($res)){
                          $returnValue['username'] = $row['username'];
                          $returnValue['user_scid'] = $row['user_scid'];
                      }
                  }
                  echo json_encode($returnValue);
                  ?>
                  

                  推荐答案

                  您要执行的任务是带有可变数量占位符的准备好的语句.这在 PDO 中更简单,但我将向您展示 mysqli 面向对象风格的方法.不管怎样,总是打印一个 json 编码的数组,这样你的接收脚本就知道需要什么样的数据类型.

                  The task you are to perform is a prepared statement with a variable number of placeholders. This is simpler in PDO, but I'll show you the mysqli object-oriented style approach. No matter what, always print a json encoded array so that your receiving script knows what kind of data type to expect.

                  我有一个片段,其中包括一整套诊断和错误检查.我没有测试过这个脚本,但它与我的这篇文章非常相似.

                  I had a snippet laying around that includes a full battery of diagnostics and error checking. I have not tested this script, but it has quite the resemblance to this post of mine.

                  if (empty($_POST['companyname']) || empty($_POST['username'])) {  // perform any validations here before doing any other processing
                      exit(json_encode([]));
                  }
                  
                  $config = ['localhost', 'root', '', 'dbname'];  // your connection credentials or use an include file
                  $values = array_merge([$_POST['companyname']], explode(',', $_POST['username']));  // create 1-dim array of dynamic length
                  $count = sizeof($values);
                  $placeholders = implode(',', array_fill(0, $count - 1, '?'));  // -1 because companyname placeholder is manually written into query
                  $param_types = str_repeat('s', $count);
                  if (!$conn = new mysqli(...$config)) {
                      exit(json_encode("MySQL Connection Error: <b>Check config values</b>"));  // $conn->connect_error
                  }
                  if (!$stmt = $conn->prepare("SELECT user_scid, user_scid FROM linked_user WHERE company_name = ? AND username IN ({$placeholders})")) {
                      exit(json_encode("MySQL Query Syntax Error: <b>Failed to prepare query</b>"));  // $conn->error
                  }
                  if (!$stmt->bind_param($param_types, ...$values)) {
                      exit(json_encode("MySQL Query Syntax Error: <b>Failed to bind placeholders and data</b>"));  // $stmt->error;
                  }
                  if (!$stmt->execute()) {
                      exit(json_encode("MySQL Query Syntax Error: <b>Execution of prepared statement failed.</b>"));  // $stmt->error;
                  }
                  if (!$result = $stmt->get_result()) {
                      exit(json_encode("MySQL Query Syntax Error: <b>Get Result failed.</b>")); // $stmt->error;
                  }
                  exit(json_encode($result->fetch_all(MYSQLI_ASSOC)));
                  

                  如果您不想要所有这些诊断条件和评论的膨胀,这里是应该执行相同的基本等效:

                  If you don't want the bloat of all of those diagnostic conditions and comments, here is the bare bones equivalent which should perform identically:

                  if (empty($_POST['companyname']) || empty($_POST['username'])) {
                      exit(json_encode([]));
                  }
                  
                  $values = explode(',', $_POST['username']);
                  $values[] = $_POST['companyname'];
                  $count = count($values);
                  $placeholders = implode(',', array_fill(0, $count - 1, '?'));
                  $param_types = str_repeat('s', $count);
                  
                  $conn = new mysqli('localhost', 'root', '', 'dbname');
                  $stmt = $conn->prepare("SELECT user_scid, user_scid FROM linked_user WHERE username IN ({$placeholders}) AND company_name = ?");
                  $stmt->bind_param($param_types, ...$values);
                  $stmt->execute();
                  $result = $stmt->get_result();
                  exit(json_encode($result->fetch_all(MYSQLI_ASSOC)));
                  

                  这篇关于如何使用 mysqli 编写一个安全的 SELECT 查询,该查询具有可变数量的用户提供的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:警告:mysqli_stmt::bind_result():绑定变量的数量与准备 下一篇:mysqli_prepare 与 PDO

                  相关文章

                  最新文章

                  <tfoot id='oB2iE'></tfoot>

                • <small id='oB2iE'></small><noframes id='oB2iE'>

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

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