views:

92

answers:

4

Riddle me this... in the while($row = mysql_fetch_assoc($result) and $runningOK) loop, if the PHP && operator is used in place of the and the mysql_fetch_assoc fails terrible and returns just the number 1 when running. I've tried mysql_fetch_array() and in place and I still have the 1 problem. It is when, and only when, I replace the && with a and like the current while statement is that the correct rows are returned.

I had placd debug statements before, inside, and after to insure this. I would like to know if this is a PHP quirk or something I couldn't account for.

// Query
$selectQuery = "SELECT * FROM jobs_cache LIMIT 20";
// Run the Selection Query.
$result = mysql_query($selectQuery)
    or die('Query Failed: '.mysql_error());

// Loop through results.
$runningOK = TRUE;
$resubmitList = array();

while($row = mysql_fetch_assoc($result) and $runningOK)
{
    // Resubmit The Job
    try
    {
        $client->addTaskBackground($row['function_name'],$row['job_data']);
        $resubmitList[] = (string)$row['job_cache_id'];
    }
    catch(Exception $e)
    {
        echo "Error adding task for job id: " . $row['job_cache_id'];
        $runningOK = FALSE;
    }
}
+1  A: 

Try this:

while(($row = mysql_fetch_assoc($result)) and $runningOK)

There might be a mix up, as it is evaluating mysql_fetch_assoc($result) and $runningOK to TRUE, which it then assigns to $row, which in this case is the same as 1

If you add the parenthesis, you most likely can use either && or and, as you will have properly separated the two evaluations.

This is the side effect of alternative syntaxes I'm sure.

Chacha102
My money's on this explanation.
Dan Tao
@Dan, me too on this.
oni-kun
+7  A: 

It's due to operator precedence. See the manual for details... here's the relevant part.

// "&&" has a greater precedence than "and"

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))

$g = true && false;

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)

$h = true and false;

var_dump($g, $h);

bool(false)
bool(true)

So, with the &&, the result of the expression is assigned to true, which evaluates to 1. With and, it evaluates to the result of the mysql function - more what you want, I reckon.

By the way, you could also use break to eliminate the need for the $runningOK variable. To do so, remove and $runningOK from the while condition and change $runningOK = FALSE; to break; and it will end the loop if the catch block is triggered.

Alex JL
this is a snipet out of a much larger Gearman Worker Kernel, the running ok is used in a few sections to insure we have valid SQL calls and worker health. What a wonderful and clear explanation of the precedence!
Urda
Also grats on the 1000 rep :)
Urda
Thanks! I'm celebrating with ice cream.
Alex JL
+1  A: 

The assignment operator = and the two logical operators && and AND have different precedence: && is executed before =, which in turn is executed before AND

So basically the statement:

$row = mysql_fetch_assoc($result) AND $runningOK

is equal to:

($row = mysql_fetch_assoc($result)) AND $runningOK

while the statement:

$row = mysql_fetch_assoc($result) && $runningOK

is equal to:

$row = (mysql_fetch_assoc($result) && $runningOK)

In the last case you are just assigning values 1 (if mysql_fetch_assoc returns a value) or 0.

Anax
A: 

Its because the operator resolving prescedence. Check out this table to understand: http://php.net/manual/en/language.operators.precedence.php

use () as much as you need when you dont know how its resolved.

useless