久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<tfoot id='6QwOL'></tfoot>

      <small id='6QwOL'></small><noframes id='6QwOL'>

        <legend id='6QwOL'><style id='6QwOL'><dir id='6QwOL'><q id='6QwOL'></q></dir></style></legend>
        • <bdo id='6QwOL'></bdo><ul id='6QwOL'></ul>
        <i id='6QwOL'><tr id='6QwOL'><dt id='6QwOL'><q id='6QwOL'><span id='6QwOL'><b id='6QwOL'><form id='6QwOL'><ins id='6QwOL'></ins><ul id='6QwOL'></ul><sub id='6QwOL'></sub></form><legend id='6QwOL'></legend><bdo id='6QwOL'><pre id='6QwOL'><center id='6QwOL'></center></pre></bdo></b><th id='6QwOL'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6QwOL'><tfoot id='6QwOL'></tfoot><dl id='6QwOL'><fieldset id='6QwOL'></fieldset></dl></div>
      1. MySQLi:使用一個準備好的語句插入多行

        MySQLi : Inserting multiple rows with one prepared statement(MySQLi:使用一個準備好的語句插入多行)

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

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

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

                • 本文介紹了MySQLi:使用一個準備好的語句插入多行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我制作了一個腳本,用于創建一個原始查詢字符串,然后在一個語句中插入數百行.它可以工作,但不提供準備好的語句所提供的保護.然后我修改了我的腳本以添加準備好的語句.但是,它可以工作,但速度要慢得多.與原始查詢腳本相比,帶有準備好的語句的腳本插入行所需的時間要長得多,因為腳本會一次運行一行準備好的插入語句,而不是一次插入數百行.

                  I made a script that creates a raw query string and then inserts the hundreds of rows in one statement. It works but does not offer the protections that prepared statements do. I then modified my script to add prepared statements. It works however, it is significantly slower. The script with prepared statements takes much longer to insert the rows than the raw query script, due to the script running through each prepared insert statement one row at a time rather than inserting hundred of rows at a time.

                  這是準備好的語句代碼的片段:

                  Here's a snippet of the prepared statement code:

                  for( $j = 0; $j < $abilitiesMax - 2; $j++ ){
                    $stmtAbility->bind_param('iiiii', $abilityArray[$i]["match_id"] , $abilityArray[$i]["player_slot"],
                    $abilityArray[$i][$j]["ability"], $abilityArray[$i][$j]["time"], $abilityArray[$i][$j]["level"] );
                  
                    if(  !($stmtAbility->execute()) ){      
                     echo "<p>$db->error</p>";
                     echo "<p>ERROR: when trying to insert abilities query</p>";
                    }
                  }
                  

                  它完成了工作,但只有在數百次插入之后.有沒有辦法將列表或數組綁定到 bind_param() 參數,然后只運行 $stmtAbility->execute 一次或其他一些可以提高性能的方法.

                  It gets the job done but only after hundreds upon hundreds of inserts. Is there a way to bind lists or arrays to the bind_param() arguments and just run the $stmtAbility->execute one time or some other method that can speed up performance.

                  抱歉,如果之前有人問過并回答過這個問題.我環顧四周,發現了一些類似的問題,但沒有明確回答我的要求.

                  Sorry if this has been asked and answered before. I looked around for a while and found some similar questions but nothing that answered what I was asking for explicitly.

                  推薦答案

                  可以通過動態構建批量插入語句查詢來準備它,但這需要一些技巧.最重要的位是使用 str_pad() 構造可變長度的查詢字符串,并使用 call_user_func_array() 調用 bind_param() 與可變數量的參數.

                  It's possible to prepare a bulk insert statement query by constructing it on the fly, but it takes a few tricks. The most important bits are using str_pad() to construct a query string of variable length, and using call_user_func_array() to call bind_param() with a variable number of parameters.

                  function insertBulkPrepared($db, $table, $fields, $types, $values) {
                      $chunklength = 500;
                      $fieldcount = count($fields);
                      $fieldnames = '`'.join('`, `', $fields).'`';
                      $prefix = "INSERT INTO `$table` ($fieldnames) VALUES ";
                      $params = '(' . str_pad('', 3*$fieldcount - 2, '?, ') . '), ';
                      $inserted = 0;
                  
                      foreach (array_chunk($values, $fieldcount*$chunklength) as $group) {
                          $length = count($group);
                          if ($inserted != $length) {
                              if ($inserted) $stmt->close();
                              $records = $length / $fieldcount;
                              $query = $prefix . str_pad('', 3*$length + 2*($records - 1), $params);
                              #echo "
                  <br>Preparing '" . $query . "'";
                              $stmt = $db->prepare($query);
                              if (!$stmt) return false;
                              $binding = str_pad('', $length, $types);
                              $inserted = $length;
                          }
                  
                          array_unshift($group, $binding);
                          #echo "
                  <br>Binding " . var_export($group, true);
                          $bound = call_user_func_array(array($stmt, 'bind_param'), $group);
                          if (!$bound) return false;
                          if (!$stmt->execute()) return false;
                      }
                  
                      if ($inserted) $stmt->close();
                      return true;
                  }
                  

                  此函數將您的 $db 作為 mysqli 實例、表名、字段名數組和值引用的平面數組.每個查詢最多插入 500 條記錄,并在可能的情況下重用準備好的語句.如果所有插入成功,則返回 true,如果任何插入失敗,則返回 false.注意事項:

                  This function takes your $db as a mysqli instance, a table name, an array of field names, and a flat array of references to values. It inserts up to 500 records per query, re-using prepared statements when possible. It returns true if all of the inserts succeeded, or false if any of them failed. Caveats:

                  • 表名和字段名沒有轉義;我把它留給你來確保它們不包含反引號.幸運的是,它們永遠不應該來自用戶輸入.
                  • 如果$values 的長度不是$fields 長度的偶數倍,那么最終塊可能會在準備階段失敗.
                  • 同樣,在大多數情況下,$types 參數的長度應該與 $fields 的長度匹配,尤其是當它們中的一些不同時.
                  • 它不區分三種失敗方式.它也不會跟蹤成功插入的次數,也不會在出現錯誤后嘗試繼續.
                  • The table and field names are not escaped; I leave it up to you to ensure that they don't contain backticks. Fortunately, they should never come from user input.
                  • If the length of $values is not an even multiple of the length of $fields, the final chunk will probably fail at the preparation stage.
                  • Likewise, the length of the $types parameter should match the length of $fields in most cases, particularly when some of them differ.
                  • It doesn't distinguish between the three ways to fail. It also don't keep track of how many inserts succeeded, nor does it attempt to continue after an error.

                  定義此函數后,您的示例代碼可以替換為:

                  With this function defined, your example code can be replaced with something like:

                  $inserts = array();
                  for ($j = 0; $j < $abilitiesMax - 2; $j++) {
                      $inserts[] = &$abilityArray[$i]['match_id'];
                      $inserts[] = &$abilityArray[$i]['player_slot'];
                      $inserts[] = &$abilityArray[$i][$j]['ability'];
                      $inserts[] = &$abilityArray[$i][$j]['time'];
                      $inserts[] = &$abilityArray[$i][$j]['level'];
                  }
                  
                  $fields = array('match_id', 'player_slot', 'ability', 'time', 'level');
                  $result = insertBulkPrepared($db, 'abilities', $fields, 'iiiii', $inserts);
                  if (!$result) {
                      echo "<p>$db->error</p>";
                      echo "<p>ERROR: when trying to insert abilities query</p>";
                  }
                  

                  那些 & 符號很重要,因為 mysqli_stmt::bind_param 需要引用,而在最新版本的 PHP 中,call_user_func_array 沒有提供這些引用.

                  Those ampersands are important, because mysqli_stmt::bind_param expects references, which aren't provided by call_user_func_array in recent versions of PHP.

                  您沒有給我們原始準備好的語句,因此您可能需要調整表和字段名稱.看起來您的代碼也位于 $i 上的循環中;在這種情況下,只有 for 循環需要在外循環內.如果您將其他行移出循環,您將使用更多的內存來構建 $inserts 數組,以換取更高效的批量插入.

                  You didn't give us the original prepared statement, so you probably need to adjust the table and field names. It also looks like your code sits inside a loop over $i; in that case, only the for loop needs to be inside the outer loop. If you take the other lines outside the loop, you will use a bit more memory constructing the $inserts array, in return for much more efficient bulk inserts.

                  還可以重寫 insertBulkPrepared() 以接受多維數組,從而消除潛在錯誤的一個來源,但這需要在對數組進行分塊后進行展平.

                  It's also possible to rewrite insertBulkPrepared() to accept a multi-dimensional array, eliminating one source of potential error, but that requires flattening the array after chunking it.

                  這篇關于MySQLi:使用一個準備好的語句插入多行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                        <bdo id='1p1wf'></bdo><ul id='1p1wf'></ul>
                          <tbody id='1p1wf'></tbody>
                      • <i id='1p1wf'><tr id='1p1wf'><dt id='1p1wf'><q id='1p1wf'><span id='1p1wf'><b id='1p1wf'><form id='1p1wf'><ins id='1p1wf'></ins><ul id='1p1wf'></ul><sub id='1p1wf'></sub></form><legend id='1p1wf'></legend><bdo id='1p1wf'><pre id='1p1wf'><center id='1p1wf'></center></pre></bdo></b><th id='1p1wf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1p1wf'><tfoot id='1p1wf'></tfoot><dl id='1p1wf'><fieldset id='1p1wf'></fieldset></dl></div>

                            <small id='1p1wf'></small><noframes id='1p1wf'>

                          1. <tfoot id='1p1wf'></tfoot>

                            <legend id='1p1wf'><style id='1p1wf'><dir id='1p1wf'><q id='1p1wf'></q></dir></style></legend>
                            主站蜘蛛池模板: 国产欧美日韩一区二区三区 | av中文天堂 | 精品一区二区三区免费 | 热久久久久 | 久久黄色影院 | 日韩精品一区在线观看 | 一区二区国产在线 | 一级特黄视频 | 成人在线免费视频 | 国产成人精品网站 | 一级特黄视频 | 日韩av在线免费播放 | 久久久久久97 | 免费看黄色网址 | 欧美成人激情 | 激情综 | 欧美区在线 | 国产精品911 | 国产精品日韩欧美 | 欧美在线观看视频 | 午夜精品福利视频 | 亚洲精品视频免费在线观看 | 三级视频在线 | 久久精品视频免费 | 天天舔天天操 | 亚洲精品无 | 亚洲欧美日韩国产精品 | 黄色小视频免费看 | 天天精品视频 | 日本在线播放 | av免费观看网址 | 国产精品一区一区三区 | 伊人久操 | 成年人视频在线免费观看 | 中文字幕在线一区二区三区 | 97精品国产露脸对白 | 在线观看日韩视频 | 亚洲伊人av | www.草 | 免费毛片网站 | 九九热在线观看视频 |