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 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧