<bdo id='VTJsn'></bdo><ul id='VTJsn'></ul>
  • <legend id='VTJsn'><style id='VTJsn'><dir id='VTJsn'><q id='VTJsn'></q></dir></style></legend>
      <tfoot id='VTJsn'></tfoot>

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

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

        php PDO使用占位符批量插入多行

        时间:2023-09-19

      2. <tfoot id='5Fkc4'></tfoot>

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

            <small id='5Fkc4'></small><noframes id='5Fkc4'>

                1. 本文介绍了php PDO使用占位符批量插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我希望使用 PHP PDO 进行多次插入.

                  I am looking to do multiple inserts using PHP PDO.

                  我找到的最接近的答案是这个

                  The closest answer I have found is this one

                  how-to-insert-an-array-into-a-single-mysql-prepared-statement

                  但是给出的示例使用 ??而不是真正的占位符.

                  However the example thats been given uses ?? instead of real placeholders.

                  我查看了 PHP 文档站点上的占位符示例

                  I have looked at the examples on the PHP doc site for place holders

                  php.net pdo.prepared-statements

                  $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
                  $stmt->bindParam(':name', $name);
                  $stmt->bindParam(':value', $value);
                  

                  现在假设我想用数组实现上述目标

                  Now lets say I wanted to achieve the above but with an array

                  $valuesToInsert = array(
                    0 => array('name' => 'Robert', 'value' => 'some value'),
                    1 => array('name' -> 'Louise', 'value' => 'another value')
                  );
                  

                  对于 PDO 和每个事务的多个插入,我将如何处理?

                  我想它会从一个循环开始?

                  I imagine it would start of with a loop?

                  $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
                  
                  foreach($valuesToInsert as $insertRow){
                  
                      // now loop through each inner array to match binded values
                      foreach($insertRow as $column => value){
                          $stmt->bindParam(":{$column}", value);
                      }
                  }
                  $stmt->execute();
                  

                  然而,上述方法不起作用,但希望能证明我试图实现的目标

                  However the above does not work but hopefully will demonstrate what im trying to achieve

                  推荐答案

                  首先,? 符号真正的占位符(大多数驱动程序允许同时使用这两种语法,位置和命名占位符).其次,准备好的语句只不过是一种将原始输入注入到 SQL 语句中的工具——SQL 语句本身的语法不受影响.您已经拥有所需的所有元素:

                  First of all, ? symbols are real place-holders (most drivers allow to use both syntaxes, positional and named place-holders). Secondly, prepared statements are nothing but a tool to inject raw input into SQL statements—the syntax of the SQL statement itself is unaffected. You already have all the elements you need:

                  • 如何使用单个查询插入多行
                  • 如何动态生成 SQL
                  • 如何使用带有命名占位符的预处理语句.

                  将它们全部结合起来非常简单:

                  It's fairly trivial to combine them all:

                  $sql = 'INSERT INTO table (memberID, programID) VALUES ';
                  $insertQuery = [];
                  $insertData = [];
                  $n = 0;
                  foreach ($data as $row) {
                      $insertQuery[] = '(:memberID' . $n . ', :programID' . $n . ')';
                      $insertData['memberID' . $n] = $memberid;
                      $insertData['programID' . $n] = $row;
                      $n++;
                  }
                  
                  if (!empty($insertQuery)) {
                      $sql .= implode(', ', $insertQuery);
                      $stmt = $db->prepare($sql);
                      $stmt->execute($insertData);
                  }
                  

                  这篇关于php PDO使用占位符批量插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何确定在 PHP 中是否启用了 PDO? 下一篇:PHP PDO 准备好的语句需要转义吗?

                  相关文章

                  最新文章

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

                  <tfoot id='UXpXA'></tfoot>

                  1. <legend id='UXpXA'><style id='UXpXA'><dir id='UXpXA'><q id='UXpXA'></q></dir></style></legend>

                    • <bdo id='UXpXA'></bdo><ul id='UXpXA'></ul>

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