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

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

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

      2. php pdo 多数组插入

        时间:2023-10-30
        <legend id='ET2UQ'><style id='ET2UQ'><dir id='ET2UQ'><q id='ET2UQ'></q></dir></style></legend>
        • <bdo id='ET2UQ'></bdo><ul id='ET2UQ'></ul>

                <tbody id='ET2UQ'></tbody>

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

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

                1. 本文介绍了php pdo 多数组插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经玩了几个小时并试图解决这个问题,但看起来很难破解.

                  I've been playing around for few hours and trying to sort this out but looks like a hard nut to crack.

                  我可以进行单个数组插入

                  I'm able to do a single array insertion

                  $person = array('name' => 'Wendy', 'age' => '32');
                  

                  但如果我想要多个这样的:

                  but if I want multiple like this:

                  $person = array(array('name'=>'Dan', 'age'=>'30'), array('name' => 'John', 'age' => '25'), array('name' => 'Wendy', 'age' => '32'));
                  

                  它不工作?任何帮助将不胜感激.

                  It's not working? Any help would be appreciated.

                  对于多次插入:

                  public function insertPdo($table, $data){
                      try{
                          if (!is_array($data) || !count($data)) return false;
                  
                          $bind = ':' . implode(', :', array_keys($data));      
                          $sql = 'INSERT INTO ' . $table . ' (' . implode(', ',array_keys($data)) . ') ' . 'values (' . $bind . ')';
                  
                          $sth = $this->__dbh->prepare($sql);
                          $result = $sth->execute($data);
                  
                      }
                      catch(PDOException $e){
                          echo $e->getMessage();
                      }
                  }
                  

                  单次插入

                  $person = array('name'=>'Dan', 'age'=>'30');
                  $db->insertPdo('test_pdo',$person);
                  
                  // For Multi Insertion, I'm trying to use this in above function
                  foreach ($data as $row) {
                      $result = $sth->execute($row);
                  };
                  
                  $person = array(array('name'=>'Dan', 'age'=>'30'), array('name' => 'John', 'age' => '25'), array('name' => 'Wendy', 'age' => '32'));
                  $db->insertPdo('test_pdo',$person);
                  

                  还有错误:

                  错误:SQLSTATE[HY093]:参数号无效:绑定变量的数量与标记的数量不匹配

                  Error: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

                  推荐答案

                  利用 MySQL 中多次插入的插入速度(http://dev.mysql.com/doc/refman/5.0/en/insert-speed.html ),你可以使用准备好的语句构建更大的查询.这确实增加了迭代方法的复杂性,因此可能只对高需求系统或大型数据集值得.

                  To take advantage of the insert speed of multiple inserts in MySQL ( http://dev.mysql.com/doc/refman/5.0/en/insert-speed.html ), you can use a prepared statement that builds the larger query. This does add complexity over an more iterative approach, so is probably only worth it for high-demand systems or largish data sets.

                  如果您有上述建议的数据:

                  If you have your data as you proposed above:

                  $person = array(array('name'=>'Dan', 'age'=>'30'), array('name' =>
                  'John', 'age' => '25'), array('name' => 'Wendy', 'age' => '32'));
                  

                  我们希望生成如下所示的查询:

                  We're looking to generate a query that looks something like this:

                  insert into table (name, age) values (?,?), (?,?), (?,?);
                  

                  要将这些整合在一起,您需要一些与此完全不同的东西:

                  To pull this together you'll want something not totally unlike this:

                  $pdo->beginTransaction() // also helps speed up your inserts
                  $insert_values = array();
                  foreach($person as $p){
                     $question_marks[] = '(?,?)';
                     $insert_values = array_merge($insert_values, array_values($p));
                  }
                  
                  $sql = "INSERT INTO table_name (name, age) VALUES " . implode(',', $question_marks);
                  
                  $stmt = $pdo->prepare ($sql);
                  try {
                      $stmt->execute($insert_values);
                  } catch (PDOException $e){
                      // Do something smart about it...
                  }
                  $pdo->commit();
                  

                  这篇关于php pdo 多数组插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP/MySQL - “开始...提交"不工作 下一篇:调试 PDO mySql 将 NULL 插入数据库而不是空

                  相关文章

                2. <legend id='GtLht'><style id='GtLht'><dir id='GtLht'><q id='GtLht'></q></dir></style></legend>

                    <bdo id='GtLht'></bdo><ul id='GtLht'></ul>
                  <tfoot id='GtLht'></tfoot>

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

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