php - MySQL - 如何从 SELECT 查询中获取多个结果

标签 php mysql select mysqli

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

最佳答案

你能尝试一下吗:

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

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

关于php - MySQL - 如何从 SELECT 查询中获取多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51371889/

相关文章:

PHP 服务器端 SNI 支持

用于 "IN"语句的变量/字符串的 PHP PDO bindParam ...?

mysql - 使用时间维度的查询忽略 "where"子句中的条件

php - Laravel:将我的所有代码放入数据库事务中是一种好习惯吗?甚至没有数据库查询代码?

php - jQuery动态类添加

php - Laravel 5.5 Redis + Socket.IO 无法工作

php - 如何使用数组中的项目创建连续播放?

php - mysql_fetch_array() 期望参数 1 为资源、给定的 bool 值和一个

php - 使用 PHP Echos 和 mySQL 计数查询等

sql - 选择 "where clause"评估顺序