pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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="vvlhx7l" 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="j7rl75t" 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="dj7r77f" 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>
                            主站蜘蛛池模板: 发电机价格|发电机组价格|柴油发电机价格|柴油发电机组价格网 | 锤式粉碎机,医药粉碎机,锥式粉碎机-无锡市迪麦森机械制造有限公司 | 二手色谱仪器,十万分之一分析天平,蒸发光检测器,电位滴定仪-湖北捷岛科学仪器有限公司 | 健身器材-健身器材厂家专卖-上海七诚健身器材有限公司 | 磁力抛光机_磁力研磨机_磁力去毛刺机-冠古设备厂家|维修|租赁【官网】 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 天津货架厂_穿梭车货架_重型仓储货架_阁楼货架定制-天津钢力仓储货架生产厂家_天津钢力智能仓储装备 | 心肺复苏模拟人|医学模型|急救护理模型|医学教学模型上海康人医学仪器设备有限公司 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | MES系统-WMS系统-MES定制开发-制造执行MES解决方案-罗浮云计算 | PE一体化污水处理设备_地埋式生活污水净化槽定制厂家-岩康塑业 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 西门子伺服控制器维修-伺服驱动放大器-828D数控机床维修-上海涌迪 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 苏州教学设备-化工教学设备-环境工程教学模型|同科教仪 | 哈尔滨发电机,黑龙江柴油发电机组-北方星光 | 石家庄装修设计_室内家装设计_别墅装饰装修公司-石家庄金舍装饰官网 | 成都LED显示屏丨室内户外全彩led屏厂家方案报价_四川诺显科技 | 高低温万能试验机_拉力试验机_拉伸试验机-馥勒仪器科技(上海)有限公司 | 翰墨AI智能写作助手官网_人工智能问答在线AI写作免费一键生成 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | 升降机-高空作业车租赁-蜘蛛车-曲臂式伸缩臂剪叉式液压升降平台-脚手架-【普雷斯特公司厂家】 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 杭州成人高考_浙江省成人高考网上报名 | 餐饮加盟网_特色餐饮连锁加盟店-餐饮加盟官网 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 今日娱乐圈——影视剧集_八卦娱乐_明星八卦_最新娱乐八卦新闻 | 广东机电安装工程_中央空调工程_东莞装饰装修-广东粤标建设有限公司 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 标准件-非标紧固件-不锈钢螺栓-非标不锈钢螺丝-非标螺母厂家-三角牙锁紧自攻-南京宝宇标准件有限公司 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 储气罐,真空罐,缓冲罐,隔膜气压罐厂家批发价格,空压机储气罐规格型号-上海申容压力容器集团有限公司 | 板框压滤机-隔膜压滤机配件生产厂家-陕西华星佳洋装备制造有限公司 | 一路商机网-品牌招商加盟优选平台-加盟店排行榜平台 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 |