两个 mysql_fetch_array 语句
是否有任何原因导致我无法在一个 while 循环中包含两个处理两个不同 mysql 查询结果的 mysql_fetch_array 语句?
原因是我有来自 mysql 数据库的两个查询结果,每个查询结果包含两列,如下所示:
Query 1: Date, Value 1
Query 2: Date, Value 2
每个查询中的日期始终是按 1 周的定期间隔按升序排列的周结束日期。然而,对于任一查询结果,它们可能在不同的日期开始和结束。
我想构建数组以返回到日期、值 1 和值 2 的调用网页,仅当值 1 和 2 在同一时期都可用时。
我包含了一个 if / else 块,该块比较每个查询中的第一个日期,并使用 mysql_data_seek 设置一个指针,为哪个结果集具有最早的开始日期,以确保它提前到与其他记录中的第一个可用日期相对应的日期放。
因为最后一个可用日期也可能不同,我认为为了确保要返回的数组具有相同的长度(因此将哪个结果的数据更新到另一个结果的最后一个可用日期),我认为我可以按如下方式迭代两个查询结果:
$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++;
}
但是,第二个返回值数组始终返回零序列。上述代码有效吗?
非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
&&
更改为and
。不,我不是在开玩笑。&&
的优先级高于=
(但and
不是),因此$a = foo() && $b = bar()
实际上是$a = (foo() && ($b = bar()))
Change
&&
toand
. No, I'm not kidding.&&
has higher priopity then=
(butand
not) so$a = foo() && $b = bar()
would be really$a = (foo() && ($b = bar()))