douhe2305 2017-04-07 13:51
浏览 13

无法调用超过100个INSERT查询

I have to insert 1000 records to read from file and insert to db but my code is not working more than 100 times. It says internal server 500 but it works fine when I run code below 100 times. Are there any settings that I have to apply to insert more than 100 records?

Here is my code:

<?php 
    $servername = "**";
    $username = "**";
    $password = "**"; 
    $dbname = "**";

    $con=mysqli_connect($servername, $username, $password,$dbname);

    for($i=0; $i<400; $i++){

        $query = mysqli_query($con,"INSERT INTO table_name (column1, column2, column3)
                                    VALUES (value1, value2, value3)");          
    }

    $query->close();
    $con->close();
?>

It's working only 100 times. If I set loop to run more than than that, it does not work any ideas?

  • 写回答

2条回答 默认 最新

  • dqol6556 2017-04-09 22:01
    关注

    I am unsure if this will eliminate your issue, but it is always better to try to reduce total database calls in your code.

    Since you are INSERTing on the same table, you can do everything in a single query. Build your single query inside the loop and then use mysqli_query() only once after the loop is finished.

    Code:

    $values="";
    for($i=0; $i<400; $i++){
        $values.=($i==0?"",",")."('value1','value2','value3')";
    }
    $query=mysqli_query($con,"INSERT INTO table_name (column1, column2, column3) VALUES $values;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100