中的两个 mysql_fetch_array 语句

2022-01-20 00:00:00 while-loop fetch php mysql

为什么我不能在一个 while 循环中包含两个处理两个不同 mysql 查询结果的 mysql_fetch_array 语句?

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?

原因是我有两个来自 mysql 数据库的查询结果,每个查询结果包含如下两列:

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

每个查询中的日期总是以 1 周的固定间隔按升序排列的一周结束日期.但是,对于任一查询结果,它们可能在不同的日期开始和结束.

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.

我想构建数组以返回日期、值 1 和值 2 的调用网页,仅当值 1 和 2 在同一时期内可用时.

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.

我已经包含了一个 if/else 块,它比较每个查询中的第一个日期并使用 mysql_data_seek 设置一个指针,以确保它提前到对应于第一个可用日期的日期另一个记录集.

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?

非常感谢

推荐答案

&&改为and.不,我不是在开玩笑.

Change && to and. No, I'm not kidding.

&&= 具有更高的优先级(但 and 没有)所以

&& has higher priopity then = (but and not) so

$a = foo() &&$b = bar() 真的是 $a = (foo() && ($b = bar()))

相关文章