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
then mysql_fetch_assoc
fails terribly 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 an and
like the current while
statement that the correct rows are returned.
I had placed 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;
}
}
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.
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.
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.
It's 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 don't know how it's resolved.