我想循环访问我的数据库,只能按时处理100行,否则我的RAM会耗尽。
但我的问题是,我不知道,为什么我的脚本不会增加开始和结束限制。因此,我只获得一次返回,并且不会通过将开始和结束限制增加+100来循环访问我的数据库。
有人看到我的失败吗?
$count_values = mysqli_num_rows($values_gk);
$count_values = intval($count_values);
$myfile = fopen("httpdocs/wp_all_import.txt", "a");
if($values_gk === FALSE){
fwrite($myfile, "SQL Error\n");
}
$start = -100;
$end = 0;
do{
$start = $start + 100;
$end = $end + 100;
if($end > $count_values){
$end = $count_values;
}
$values_gkw = $database_gk->query("SELECT `ID` FROM `fWR6qIN_posts` where post_type = 'product' AND post_status = 'publish' limit $start, $end");
fwrite($myfile, "Entered 1. While Loop\n");
while($row = $values_gkw->fetch_assoc()){
if($row["ID"] != null){
//do something with the values
//code removed to reduce the text here
}
}
fwrite($myfile, "\n+++ Start: " .$start. " Limit: " .$end. " +++\n\n");
} while ($end <= $count_values);
计数值:(无限制地检查以获取所有行的数量)
$values_gk = $database_gk->query("SELECT `ID` FROM `fWR6qIN_posts` where post_type = 'product' AND post_status = 'publish'");
$count_values = mysqli_num_rows($values_gk);
$count_values = intval($count_values);
问候,谢谢!
答案 0 :(得分:0)
限制语法为LIMIT offset, count
NOT LIMIT offset_start, offset_end
。
它将在循环的第一次迭代中选择100
行,但在第二次循环迭代中将选择200
行,在第二次迭代中选择$end
= 200,在第三次迭代中选择300行,依此类推。 / p>
根据kalrodan的规定,您的代码会产生无限循环。
我在代码中做了几处更正。试试这个:
$start = -100;
$limit = 100;//constant never changes
$count = 0;
do{
$start = $start + 100;
$count = $count + 100;//see changes here
if($count > $count_values){//see changes here
$limit = $count_values - $start;//see changes here
}
$values_gkw = $database_gk->query("SELECT `ID` FROM `fWR6qIN_posts` where post_type = 'product' AND post_status = 'publish' limit $start, $limit");//$start, $limit here
fwrite($myfile, "Entered 1. While Loop\n");
while($row = $values_gkw->fetch_assoc()){
if($row["ID"] != null){
//do something with the values
//code removed to reduce the text here
}
}
fwrite($myfile, "\n+++ Start: " .$start. " Limit: " .$end. " +++\n\n");
} while ($count < $count_values);//condition change here too