mysql_fetch_assoc() 仅适用于 WHILE 循环中的一次迭代
请考虑以下事项:
$query = "SELECT legIDs FROM trip";
$result = mysql_query($query) or die("SELECT TRIPS ERROR: " . mysql_error());
while($row = mysql_fetch_assoc($result) or die("fetch error " . mysql_error())) {
echo "<div style=\"border: 1px solid blue;float:left;\">Trip: <div style=\"float:right;\">";
$legID = explode(",", $row['legIDs']);
foreach($legID as $leg) {
$query = "SELECT dep, arr FROM leg WHERE `Key` = " . $leg;
$result = mysql_query($query) or die("SELECT LEGS ERROR: " . mysql_error());
$row2 = mysql_fetch_assoc($result) or die("FILL ARRAY ERROR: " . mysql_error());
echo $row2['dep'] . " - " . $row2['arr'] . "<br />";
}
echo "</div></div>";
}
由于某种原因,即使 WHILE()
循环返回多个值,该函数也仅返回一个结果。 FOREACH()
循环作用于返回的一行。我做错了什么只返回一行吗?直接在服务器上执行 $result
中包含的查询会返回几行。
Please consider the following:
$query = "SELECT legIDs FROM trip";
$result = mysql_query($query) or die("SELECT TRIPS ERROR: " . mysql_error());
while($row = mysql_fetch_assoc($result) or die("fetch error " . mysql_error())) {
echo "<div style=\"border: 1px solid blue;float:left;\">Trip: <div style=\"float:right;\">";
$legID = explode(",", $row['legIDs']);
foreach($legID as $leg) {
$query = "SELECT dep, arr FROM leg WHERE `Key` = " . $leg;
$result = mysql_query($query) or die("SELECT LEGS ERROR: " . mysql_error());
$row2 = mysql_fetch_assoc($result) or die("FILL ARRAY ERROR: " . mysql_error());
echo $row2['dep'] . " - " . $row2['arr'] . "<br />";
}
echo "</div></div>";
}
For some reason this only returns one result, even when there are several values returned from the WHILE()
loop. The FOREACH()
loop works on the one row that's returned. Am I doing something wrong to only have this return one row? Performing the query contained in $result
directly on the server returns several rows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在内部循环和外部循环中使用了
$result
两次。当内部循环完成时,$result 将是一个“空”结果集,因此外部循环将终止。将内部循环更改为$result2
,事情应该没问题。但是,由于内部循环只是根据外部循环生成的内容获取更多结果,因此您应该考虑将其重写为连接查询。运行单个“大结果”查询几乎总是比运行一长串“小结果”单独查询更有效。您最终会花费相当大的开销来解析/编译每个内部查询。
You're using
$result
twice in the inner and outer loops.. When the inner loop completes, $result will be an "empty" result set, so the outer loop will terminate. CHange the inner loop to$result2
and things should be fine.However, since the inner loop is simply fetching more results based on what the outer loop produces, you should consider rewriting it as a joined query. It's almost always more efficient to run a single "large result" query, than a long series of "small result" individual queries. You end up with fairly hefty overhead to parse/compile each of the inner queries.
这是相当混乱的事情。
while($row = mysql_fetch_assoc($result) 或 die("FETCH ERROR: " .mysql_error()))
不需要
或die()
。如果mysql_fetch_assoc
返回false
,则while
循环应该结束,你也不需要die
.$result
变量,这可能是您所询问的特定问题的原因。中。混合表示逻辑和后端逻辑很快就会变得非常混乱。
This is quite messy stuff there.
while($row = mysql_fetch_assoc($result) or die("FETCH ERROR: " . mysql_error()))
There's no need for the
or die()
. Ifmysql_fetch_assoc
returnsfalse
, thewhile
loop is supposed to end, you don't need todie
as well.$result
variable of the outer loop in the inner loop, which is probably the cause for the particular problem you're asking about.<div>
s. Mixing presentation logic and backend logic gets very messy very quickly.