Reputation: 33
Is there any reason why I couldn't include two mysql_fetch_array statements working on two different mysql query results in one while loop?
The reason is I have two query results from a mysql database each containing two columns as follows:
Query 1: Date, Value 1
Query 2: Date, Value 2
Dates in each query are always week ending dates at regular intervals of 1 week ordered in ascending order. However they may start and finish at different dates for either query result.
I want to build arrays to return to the calling web page of date, value 1 and value 2 only where both values 1 and 2 are available over the same period.
I have included an if / else block that compares the first date in each query and sets a pointer using mysql_data_seek for which ever result set has the earliest start date to ensure it is advanced to the date corresponding to the first available date in the other record set.
Because the last available date may also be different, I thought that to ensure the arrays to be returned are both of the same length (therefore truncating whichever result has the more recent data to the last available date of the other result), I thought that I could iterate through both query results as follows:
$ReturnDate = array();
$ReturnValue1 = array();
$ReturnValue2 = array();
$i=0;
while ($row1=mysql_fetch_array($res_Query1,MYSQL_NUM) && $row2=mysql_fetch_array($res_Query2,MYSQL_NUM)) {
$ReturnDate[$i]= $row1[0];
$ReturnValue1[$i] = (float)$row1[1];
$ReturnValue2[$i] = (float)$row2[1];
$i++;
}
However, the second return value array always returns a sequence of zeros. Is the above code valid?
Many thanks
Upvotes: 3
Views: 2829
Reputation: 2343
Change &&
to and
. No, I'm not kidding.
&&
has higher priopity then =
(but and
not) so
$a = foo() && $b = bar()
would be really $a = (foo() && ($b = bar()))
Upvotes: 4