Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two mysql_fetch_array statements in

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

like image 804
nortyrich Avatar asked Apr 19 '26 21:04

nortyrich


1 Answers

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()))

like image 196
Emmerman Avatar answered Apr 21 '26 12:04

Emmerman