Two mysql_fetch_array statements in

百般思念 提交于 2019-12-22 10:46:29

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/5801087/two-mysql-fetch-array-statements-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!