PHP + MySQL Timeout Error
-
I'm working on a script for my employer's website that will take form data and enter it into a MySQL database. Easy enough...until they wanted it to never have any gaps in the "id" field. They want the "id" field to be automatically generated and for it to never have any gaps in it if we decide to delete someone out of the database for whatever reason. To do this I wrote the following code (this isn't in it's entirety, just the pieces that are pertinent.)
$id = 0; $query = mysql_query("SELECT * FROM ORDER BY id") or die(mysql_error()); $row = mysql_fetch_array($query) or die(mysql_error()); // This while loop checks the 'id' field and finds if there are any gaps, automatically // setting the $id variable to the smallest unused value while( $row['id'] = ($id + 1)){ $id += 1; } $id +=1;
It looks to me that it should say "while the id field (1) = $id + 1 (1) then add 1 to $id and repeat until the id field doesn't = $id + 1 anymore." For some reason though, when I run the script from the website it just sits there until I get this error back from Firefox. Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/www.website.com/htdocs/script.php on line 33 If I take the while statement out of the code and just have it echo back the values of $row['id'] and $id + 1 it brings back 1 and 1. But if I add the while loop in there it errors out again. I've tried while, do while, and if statements all to no avail. My apologies for the lengthiness, but I wasn't sure how to make this more concise. -
I'm working on a script for my employer's website that will take form data and enter it into a MySQL database. Easy enough...until they wanted it to never have any gaps in the "id" field. They want the "id" field to be automatically generated and for it to never have any gaps in it if we decide to delete someone out of the database for whatever reason. To do this I wrote the following code (this isn't in it's entirety, just the pieces that are pertinent.)
$id = 0; $query = mysql_query("SELECT * FROM ORDER BY id") or die(mysql_error()); $row = mysql_fetch_array($query) or die(mysql_error()); // This while loop checks the 'id' field and finds if there are any gaps, automatically // setting the $id variable to the smallest unused value while( $row['id'] = ($id + 1)){ $id += 1; } $id +=1;
It looks to me that it should say "while the id field (1) = $id + 1 (1) then add 1 to $id and repeat until the id field doesn't = $id + 1 anymore." For some reason though, when I run the script from the website it just sits there until I get this error back from Firefox. Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/www.website.com/htdocs/script.php on line 33 If I take the while statement out of the code and just have it echo back the values of $row['id'] and $id + 1 it brings back 1 and 1. But if I add the while loop in there it errors out again. I've tried while, do while, and if statements all to no avail. My apologies for the lengthiness, but I wasn't sure how to make this more concise.while( $row['id'] = ($id + 1)){
don't you think that you are using ASSIGNMENT operator instead of EQUALITY COMPARISON operator i.e. = instead of ==Ashish Sehajpal
-
while( $row['id'] = ($id + 1)){
don't you think that you are using ASSIGNMENT operator instead of EQUALITY COMPARISON operator i.e. = instead of ==Ashish Sehajpal
Ah...wow I feel dumb. Yeah that was the problem...the script works beautifully now. I guess I skipped that chapter when I was teaching myself PHP. Thanks!