Really stupid debugging question because I'm losing my mind

I’m trying to debug some code from previous developer (ie I wouldn’t normally program like this) and I’ve got an error I just can’t get rid of. The code below results in the output beneath it.

What I can’t figure out is how execution gets to the point of error, because the query result has already been checked (and output). Basically I’m confused and am losing the plot. Can anyone help?

$res   = mysql_query($query);
if(!$res) continue; //skip out of this loop iteration

$rows = mysql_num_rows($res);

if ($rows>0)
    echo "Res : ";
    while ($row = mysql_fetch_array($res)) //this is line 125

Res : resource(22) of type (mysql result)
PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in <snip />/getDetails.php on line 125

Try this

While ($res && $row= ...

Good suggestion. Still goes through and errors:

echo "\
also checking res in while condition : \
while ($res && $row = mysql_fetch_array($res)){
also checking res in while condition : 
PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in myscript.php on line 126

you say it’s in the loop. My guess is that the first loop is executing successfully but the second (or last) query is returning an invalid result.

I suspect $res is being left over from a previous iteration.
Try this:

$res   = mysql_query($query); 
if(!$res) {
echo mysql_error();
continue; //skip out of this loop iteration 

OK try again :slight_smile:

while ( is_resource($res) && $row = mysql_fetch_array($res))

Well yeah, that seems to have done the trick. In that it’s not giving an error because it doesn’t try to execute the mysql_fetch_array. Even though it probably should. :confused:

I think Tom is probably right in that variables are being overwritten by previous iterations and other looping structures.

I think this code is too far gone to be able to live. I was hoping to understand and fix this bug but I think I’m going to rip it all out and start again.

Have you tried installing xdebug and using breakpoints to walk through the code a line at a time to see what’s going on?

if (!$res)

If the result of the MySQL query fails, why would you want to continue and try to extract results? (From a query result that doesn’t exist). Or am I reading things wrong?

You’re reading it wrong, the continue keyword skips the rest of the current iteration and moves on to the next one.


for ($i = 0; $i < 5; $i++ ) {

      if ($i == 2) continue;

      echo $i . "\




I can understand that example. But in the OP’s code there IS no iteration as such … it is a single expression that is not within a loop.

I’m assuming that hessodreamy had posted a snippet and simply implied the loops existence. It clearly isn’t the full set of code. Though, correct me if I’m wrong, @hessodreamy !