首页 > 解决方案 > MySQL - 如何从 SELECT 查询中获取多个结果

问题描述

此代码仅显示一行。当我执行其中一个查询时,如何显示 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();

标签: phpmysqlselectmysqli

解决方案


你能试试:

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

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


推荐阅读