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

时间:2018-07-17 01:10:18

标签: php mysql select mysqli

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

// 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 个答案:

答案 0 :(得分:1)

您可以尝试:

param["userType"] = "ADMIN"

并删除所有其他 while ($row = $queryresults->fetch_assoc()) { /* do stuff with the $row */ } 分配。我认为您调用fetch_assoc()的方式有误