mysql_fetch_assoc() 仅适用于 WHILE 循环中的一次迭代

发布于 2024-11-15 03:42:36 字数 943 浏览 1 评论 0原文

请考虑以下事项:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你列表最软的妹 2024-11-22 03:42:36

您在内部循环和外部循环中使用了 $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.

超可爱的懒熊 2024-11-22 03:42:36

这是相当混乱的事情。

  1. while($row = mysql_fetch_assoc($result) 或 die("FETCH ERROR: " .mysql_error()))

    不需要或die()。如果mysql_fetch_assoc返回false,则while循环应该结束,你也不需要die .

  2. 基于其他 SQL 查询的循环内的 SQL 查询通常可以通过原始查询中更好的 JOIN 或通过进行更智能的辅助查询来更好地处理。
  3. 您正在内部循环中覆盖外部循环的 $result 变量,这可能是您所询问的特定问题的原因。
  4. 您应该将数据获取与输出分开。首先执行查询以获取所有必要的数据,然后将其输出到
    中。混合表示逻辑和后端逻辑很快就会变得非常混乱。

This is quite messy stuff there.

  1. while($row = mysql_fetch_assoc($result) or die("FETCH ERROR: " . mysql_error()))

    There's no need for the or die(). If mysql_fetch_assoc returns false, the while loop is supposed to end, you don't need to die as well.

  2. SQL queries within loops based on other SQL queries are usually better handled via better JOINs in the original query or by making more intelligent secondary queries.
  3. You're overwriting your $result variable of the outer loop in the inner loop, which is probably the cause for the particular problem you're asking about.
  4. You should separate data fetching from outputting. Do your queries first to fetch all necessary data, then output it into <div>s. Mixing presentation logic and backend logic gets very messy very quickly.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文