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

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

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题