doujiao7520 2018-07-16 17:10
浏览 233
已采纳

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

This code is only displaying one row. How can I show the other rows that are in the MySQL table when I execute one of the queries? Thank you for anything you can offer! Help is appreciated!

// Start query
$connection = new mysqli($host, $user, $password, $db, $port);
if ($connection->connect_error) { 
    die("Connect Error: (" . $connection->connect_errno . ") ". $connection->connect_error());
}
$month = $_GET["month"] ?? 'All';
$day = $_GET["day"] ?? 'All';

$a = "SELECT name, starthour, startmin, ampm, hours, minutes, endhour, endmin, endampm ";
$a .= "FROM Lessons WHERE (month='$month' AND day='$day')";
$b = "SELECT name, starthour, startmin, ampm, hours, minutes, endhour, endmin, endampm FROM Lessons";
$query = (($month != 'All') and ($day != 'All')) ? $a : $b;
$queryresults = $connection->query($query);

// 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();

展开全部

  • 写回答

1条回答 默认 最新

  • dongse5528 2018-07-16 17:38
    关注

    Could you try:

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

    And remove every other $row assignment. I think there is a mistake in the way you are calling fetch_assoc()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部