代码之家  ›  专栏  ›  技术社区  ›  ReinstateMonica3167040 urvashi bhagat

mysql-如何从select查询中获取多个结果

  •  0
  • ReinstateMonica3167040 urvashi bhagat  · 技术社区  · 6 年前

    此代码只显示一行。当我执行其中一个查询时,如何显示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 回复  |  直到 5 年前
        1
  •  1
  •   ReinstateMonica3167040 urvashi bhagat    6 年前

    你能试试吗:

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

    把其他人都带走 $row 转让。我认为你称呼fetch_assoc()的方式有错误。