Php mysql query infinite loop

software development

#1

I was working on a php script last night and I forgot to change a variable in a while loop I was adapting. Here’s how it looked:

$recEngID = strtok($recEngList, ","); while ($recEngID !== false) { $query = "INSERT INTO SongRecEngRelation "; $query .= "(SongID, RecEngID) "; $query .= "VALUES ($songID, $recEngID)"; $result = mysql_query($query); $albumID = strtok(","); }So, $albumID should have been $recEngID, but because it wasn’t, the loop became an infinite one, sending identical query after identical query. I tried to kill it through the shell and the web panel, but couldn’t figure out how to get to a cgi process. The inserts finally stopped after a few hundred thousand queries, because the connection was closed by the database, I assume.

Is there anyway to kill the process if I make the same mistake again?

gabe


#2

while ($recEngID !== false) {

That line is wrong.
First off, it isn’t being changed in the loop, to my knowledge. So this means it will loop indefinatly.
I’ve never used that statement before… I’m guessing if it’s not exactly identical to? I’ve used != successfully before but never tried !==.


#3

I’m not aware of any way to stop it once it’s started, though there may be a way.

However, what I do know is that you can use the set_time_limit function to cause your scripts to timeout after a certain number of seconds. If you’re concerned about infinite loops due to coding errors you can set this to 1 or 2 seconds until you are done developing your app.


#4

You’re right,

while ($recEngID !== false) {is wrong, but it works correctly as written with the variable switched. I’ll change it in my script anyway.

Thanks.


#5

gsf,

You shouldn’t change it. The way you’ve coded it is correct, and if you changed it, it could malfunction. From the PHP manual on strtok:

[quote]This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or “”.

Use the === operator for testing the return value of this function.[/quote]
the !== operator is the negative form of the === operator, and therefore your code checks the value appropriately.


#6

Thanks for the correction, jburbage. I’ve changed them back to strict comparison operators (!==).

I’ve also thrown a set_time_limit(30) in at the top of the program to put a leash on any more infinite loops I might create.