Query mysql and run a script for each member. (BIG DATABASE)
So I have a fairly large database of ~800,000 members
I want to run a query on the database and run a script for each member.
How can I do that without timing out?
This isn't wor开发者_StackOverflow中文版king for me:
$query = 'SELECT * FROM members';
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
//script
}
You can use set_time_limit(0)
which means there is no timelimit on the file being run.
See set_time_limit($seconds)
The maximum execution time, in seconds. If set to zero, no time limit is imposed.
The other risk you run is memory issues. You can raise the memory limit, but you are better off processing the data in chunks, using limits to prevent loading too much data at once.
Here's an example:
<?php
$start = 0;
$size = 100;
while(true) {
$query = sprintf('SELECT * FROM members LIMIT %d,%d', $start, $size);
$result = mysql_query($query);
if (mysql_num_rows($result) == 0)
break;
while ($row = mysql_fetch_assoc($result)) {
//script
}
mysql_free_result($result);
$start += $size;
}
精彩评论