我正在做一个项目。我需要查询数据库并将结果写入 csv 文件。结果将超过 15,000 个条目(这就是用户想要的)。我正在使用 LIMIT 分解结果,因为如果我不这样做,数据库将超时。我将查询划分为我所说的 total_pages。这是我的代码。
大for循环,循环19次。问题是代码将通过嵌套循环一次(只有 500 个条目)然后它不会返回。我尝试在 $results 上使用 null 但没有运气。请帮忙。
// using this for my LIMIT
$start_from = 0;
$sql = "select * from someplace where gender = '".$gender."'";
$rs_result = mysql_query($sql);
$total_records = mysql_num_rows($rs_result);
$total_pages = ceil($total_records / 500);
// open a file
//write x in file
file = fopen("./DataFile/gdownload.csv","w");
//write header to the file
$x = "Last Name,First Name,Primary_Name, ........ etc...... \n";
fwrite($file, $x);
for($count = 0; $count <= $total_pages; $count++)
{
$query = "SELECT *
FROM ptable
JOIN person_name ON ptable.Primary_Name = person_name.Primary_Name
WHERE gender = '$gender'
ORDER BY person_name.Lname ASC
LIMIT ".$start_from.", 500";
$result = mysql_query($query) or die(mysql_error());
$num_row = mysql_num_rows($result);
//print tables in rows
while($row = mysql_fetch_array($result))
{
$x="";
$x=$x.$row['Lname'].",";
$x=$x.$row['Fname'].",";
$x=$x.$row['Primary_Name'].",";
$x=$x.$row['asdf#'].",";
$x=$x.$row['qwer'].",";
$x=$x.$row['hjkl'].",";
$x=$x.$row['bnm,'].",";
$x=$x.$row['yui'].",";
$x=$x.$row['aaa'].",";
.....
fwrite($file, $x);
}// end nested while
$start_from+=500;
}// end for loop
fclose($file);