我的PHP脚本超时时收到内部服务器错误。我从mySQL数据库中获取8000行,并将每行分成3个不同的表。
我做了一些关于堆栈的研究,并作为解决方案遇到了这个问题:
set_time_limit(0);
ignore_user_abort(1);
现在,我只是没有在chrome中收到错误,但仍然没有添加到数据库中。
以下是我正在做的一些伪说:
$db = new myConnectDB();
$query = "SELECT * FROM tableName";
$result = $db->query($query);
$numRows = $result->num_rows;
//beerName', '$style' , '$brewery', '$abv','$ibu','$breweryID', '$icon', '$iconBrew' , '$lat', '$long' , '$beerRating')";
for ($i=0; $i < 8000 ; $i++) {
//fetch associate
$row = $result->fetch_assoc();
//get all 13 variables from rom
$db2 = new myConnectDB();
//insert some variables into 1st table
mysqli_close($db2);
//todo: insert BeerTable / check and
$db3 = new myConnectDB();
//insert variables into second table
mysqli_close($db3);
//todo: insert BreweryTable / check and update
$db4 = new myConnectDB();
//insert variables into third table.
mysqli_close($db4);
}
echo $i;
答案 0 :(得分:2)
在循环内建立和断开连接是不好的做法。尝试以这种方式重构代码:
$db = new myConnectDB();
$db2 = new myConnectDB();
$db3 = new myConnectDB();
$db4 = new myConnectDB();
$query = "SELECT * FROM tableName";
$result = $db->query($query);
$numRows = $result->num_rows;
$insertQuery1 = "INSERT INTO tbl1 (col1, col2, col3) VALUES";
$insertQuery2 = "INSERT INTO tbl2 (col1, col2, col3) VALUES";
$insertQuery3 = "INSERT INTO tbl3 (col1, col2, col3) VALUES";
for ($i=0; $i < 8000 ; $i++) {
//fetch associate
$row = $result->fetch_assoc();
//get all 13 variables from rom
//insert some variables into 1st table
$insertQuery1 .= '(' . $val1 . ',' . $val2 . ',' . $val3 ')';
//todo: insert BeerTable / check and
//insert variables into second table
$insertQuery2 .= '(' . $val4 . ',' . $val5 . ',' . $val6 ')';
//todo: insert BreweryTable / check and update
//insert variables into third table.
$insertQuery1 .= '(' . $val7 . ',' . $val8 . ',' . $val9 ')';
}
$db1->query($insertQuery1);
$db2->query($insertQuery2);
$db3->query($insertQuery3);
mysqli_close($db2);
mysqli_close($db3);
mysqli_close($db4);
在这个概念中,您不会为每行查询3个插入内容。您收集3个大插入查询并在循环后执行它们。所以这段代码会更快。