【问题标题】:MySQL - how to get multiple results from SELECT queryMySQL - 如何从 SELECT 查询中获取多个结果
【发布时间】:2018-07-17 01:10:18
【问题描述】:

此代码仅显示一行。当我执行其中一个查询时,如何显示 MySQL 表中的其他行? 感谢您提供的任何东西!感谢您的帮助!

// Display query results in a table
if ($queryresults) {
    $row = $queryresults->fetch_assoc(); // Problem is here or below
    echo "<table> <tbody><tr><th>Name</th><th>Start Time</th>";
    echo "<th>Duration</th><th>End Time</th>";
    while($row) {
        // Create row of table
        $str = "<tr><td>". $row['name']."</td><td>". $row['starthour'].":";
        $str .= format2($row['startmin'])." ". $row['ampm']."</td><td>". $row['hours'];
        $str .= "h ". format2($row['minutes'])."m</td><td>". $row['endhour'].":";
        $str .= format2($row['endmin'])." ". $row['endampm'] . "</td></tr>";
        echo $str;
        $row = $queryresults->fetch_assoc($queryresults);
    }
    echo "</tbody></table>";
} else {
    echo "Error: #".$connection->errno." – ".$connection->error;
}
// Logout of server
$connection->close();

【问题讨论】:

  • 如果需要,我很乐意提供更多信息!
  • 您不能使用 query($query) 运行多个查询。它只运行 $a
  • 我很困惑这行: $query = (($month != 'All') and ($day != 'All')) ? $a : $b;
  • 我不确定是否可以将此标记为与此重复 stackoverflow.com/questions/51286142/…

标签: php mysql select mysqli


【解决方案1】:

你可以试试:

 while ($row = $queryresults->fetch_assoc()) {
 /* do stuff with the $row */
 }

并删除所有其他 $row 分配。我认为您调用 fetch_assoc() 的方式有误

【讨论】:

  • 现在按预期工作!我想知道为什么它以前不起作用。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2011-07-26
  • 2021-06-15
相关资源
最近更新 更多