doujiao7520
2018-07-17 01:10
浏览 207
已采纳

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-17 01: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()

    点赞 评论

相关推荐 更多相似问题