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

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

        <legend id='OYjAY'><style id='OYjAY'><dir id='OYjAY'><q id='OYjAY'></q></dir></style></legend>
        <tfoot id='OYjAY'></tfoot>

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

        PHP在后台向DB中插入批量数据

        时间:2023-07-30
        <legend id='xUlcg'><style id='xUlcg'><dir id='xUlcg'><q id='xUlcg'></q></dir></style></legend>
      1. <tfoot id='xUlcg'></tfoot>
        • <bdo id='xUlcg'></bdo><ul id='xUlcg'></ul>
              <tbody id='xUlcg'></tbody>

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

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

                  本文介绍了PHP在后台向DB中插入批量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个向客户发送批量消息的表单,点击提交按钮,它在发送前将消息保存在数据库中,但插入过程需要大约 2 分钟才能插入 3000 条记录,如何我可以减少插入时间或者我如何在后台处理数据以避免用户等待进程完成.我已经尝试了几种堆栈溢出的选项,但都没有成功.我在共享主机上.这是我的代码

                  I have a form which sends bulk messages to customers, on clicking submit button, it saves the messages in the DB before sending, but the inserting process takes about 2 mins to insert 3000 records, How can I reduce the insertion time or how can I process the data in the background to avoid the user waiting for the process to complete. I have tried several options on stack overflow with no success. I am on a shared hosting. here is my code

                  <?php
                  if(isset($_POST['submit'])){
                   $date = date("D, F d, Y h:i:sa");
                   $phones = $_POST['recipient_phones']; //get phone numbers from textarea
                   $phones = trim($phones,",
                  	
                  /s+/x0B]/"); //do some regex
                   $phones = multiexplode(array(","," ","
                  ","r
                  ",".","|",":"),$phones);//reformat and convert to array
                   $sender = $_POST['sender_id']; //Get Sender ID input field
                   $message = $_POST['message']; //Get Message input field
                   $time = $_POST['sc_time']; //Get time input field
                  
                  
                  
                   ob_end_clean();
                   ignore_user_abort();
                   ob_start();
                   header("Connection: close");
                   // echo json_encode($out);
                   header("Content-Length: " . ob_get_length());
                   ob_end_flush();
                   flush();
                  
                  
                  
                  foreach($phones as $phone){
                  
                        $data = array("sender" => "$sender","phone" => "$phone", "message" => "$message", "user_id" => "$user_id","time_submitted" => "$date");
                     
                           $qry = Insert('crbsms_queue',$data);
                    
                     
                     $_SESSION['msg']="10";
                  
                    echo "<script>location.href='$url?success=yes';</script>";
                     exit
                  }
                  
                  
                  
                       # Insert Data 
                      function Insert($table, $data){
                      global $mysqli;
                      //print_r($data);
                  
                      $fields = array_keys( $data );  
                      $values = array_map( array($mysqli, 'real_escape_string'), array_values( $data ) );
                      
                     //echo "INSERT INTO $table(".implode(",",$fields).") VALUES ('".implode("','", $values )."');";
                     //exit;  
                      mysqli_query($mysqli, "INSERT INTO $table(".implode(",",$fields).") VALUES ('".implode("','", $values )."');") or die( mysqli_error($mysqli) );
                  
                  }
                  

                  推荐答案

                  插入 3000 行并不多,如果操作得当,应该不会花费太多时间.您必须记住,您应该始终使用准备好的语句.您可以使用不同的数据多次执行相同的语句.当你将整个事情包装在一个事务中时,它应该执行得非常快.

                  Inserting 3000 rows is not a lot and it should not take too much time if you do it properly. You must remember that you should always use prepared statements. You can execute the same statement multiple times with different data. When you wrap the whole thing in a transaction it should be executed really fast.

                  // Start transaction
                  $mysqli->begin_transaction();
                  
                  // prepared statement prepared once and executed multiple times
                  $insertStatement = $mysqli->prepare('INSERT INTO crbsms_queue(sender, phone, message, user_id, time_submitted) VALUES(?,?,?,?,?)');
                  $insertStatement->bind_param('sssss', $sender, $phone, $message, $user_id, $date);
                  foreach ($phones as $phone) {
                      $insertStatement->execute();
                  }
                  
                  // Save and end transaction
                  $mysqli->commit();
                  

                  如果这不能提高性能,则意味着您在其他地方遇到了问题.您需要分析和调试您的应用程序以找出问题的来源.

                  If this doesn't improve the performance then it means you have a problem somewhere else. You need to profile and debug your application to find where the issue comes from.

                  附注:记得启用mysqli错误报告,否则您的交易可能无法正常运行.

                  Side note: Remember to enable mysqli error reporting, otherwise your transaction might not behave properly.

                  这篇关于PHP在后台向DB中插入批量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将其从 MySQL 更改为 MySQLi? 下一篇:执行 mysqli_query 时遇到问题

                  相关文章

                  最新文章

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

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