我正在开发一个项目。我需要查询一个数据库并将结果写入一个csv文件。结果将超过15,000个条目,(这就是用户想要的)。我使用LIMIT分解结果,因为如果我不这样,数据库会超时。我将查询划分为我所谓的total_pages。这是我的代码。PHP查询时嵌套问题
大的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);
你是什么意思“不回去”?我看到一个for循环会执行'ceil($ total_records/500)+ 1'运行。那19个在哪里? – Nanne 2012-04-12 14:03:44
当我只是这个搜索total_pages是一个计数,所以现在我会多少次通过for循环。当我最后一次尝试时,共有19页。 – user1329251 2012-04-12 14:21:02
啊,检查,brainfail @' ceil'和'/'对我来说,别介意:) – Nanne 2012-04-12 14:21:42