简体   繁体   中英

PHP + MySQL - While Loop not

I'm quite new to PHP + MySQL so, please pardon my confusion — I don't understand why my code won't loop through the 'underground_name' column and echo all the rows?

Here's my code...

<?php
    include("../includes/header.php");
    require('../../mysqli_connect.php');
    include("../functions/filter_time.php");

    // query the database
    $query = "SELECT * FROM projects_underground, underground, projects
            LEFT JOIN river ON projects.river_id=river.river_id 
            LEFT JOIN dlr ON projects.dlr_id=dlr.dlr_id 
            LEFT JOIN overground ON projects.overground_id=overground.overground_id 
            LEFT JOIN natrail ON projects.natrail_id=natrail.natrail_id 
            LEFT JOIN tram ON projects.tram_id=tram.tram_id

            WHERE 
            projects_underground.underground_fk = underground.underground_id AND
            projects_underground.projects_fk = projects.projects_id AND
            name = 'Imperial War Museum'";

    $result = mysqli_query($dbc, $query); // put queried result into variable
    $row = mysqli_fetch_assoc($result); // put that into seperate arrays called $row
    $ug = mysqli_fetch_array($result);

    while ($ug = mysqli_fetch_array($result)) {
        echo $ug['underground_name'];
    }
?>

Thanks for your help in advance!

  1. Try running your query on the SQL console, and check what results you get.
  2. Try dumping the $ug array to see if the output is being fetched.
  3. You're calling mysqli_fetch_*() thrice, once while initializing $row , once while initializing $ug , and once more in the while loop. This will miss the first two rows - and consequently, if you have only two rows in the query result, no output would be printed.


$row = mysqli_fetch_assoc($result); // Fetches first result row off the stack 
$ug = mysqli_fetch_array($result);  // Fetches second result row

while ($ug = mysqli_fetch_array($result)) { // Fetches third row
     // Starts printing from the third row (if there is any)
     echo $ug['underground_name'];
}

You need to understand that when you call mysqli_fetch_assoc($result); or mysqli_fetch_array($result); that you are fetching a row off the result stack. So before you ever reach the while statement you've already gotten two rows off. So if your query returned three results, you'd only show one in your loop.

Try running the following to see how many rows are being returned:

echo $result->num_rows().' rows have been returned.'.PHP_EOL;

Here's an update to your code that I think should do the trick (by removing the earlier calls to fetch_assoc and fetch_array and adding some basic checking that any rows have been returned):

<?php
include("../includes/header.php");
require('../../mysqli_connect.php');
include("../functions/filter_time.php");

// query the database
$query = "SELECT * FROM projects_underground, underground, projects
        LEFT JOIN river ON projects.river_id=river.river_id 
        LEFT JOIN dlr ON projects.dlr_id=dlr.dlr_id 
        LEFT JOIN overground ON projects.overground_id=overground.overground_id 
        LEFT JOIN natrail ON projects.natrail_id=natrail.natrail_id 
        LEFT JOIN tram ON projects.tram_id=tram.tram_id

        WHERE 
        projects_underground.underground_fk = underground.underground_id AND
        projects_underground.projects_fk = projects.projects_id AND
        name = 'Imperial War Museum'";

$result = mysqli_query($dbc, $query); // put queried result into variable

if (is_object($result) && $result->num_rows() > 0) {

    // This line is just for testing, delete from real code
    echo $result->num_rows().' rows have been returned.'.PHP_EOL;

    while ($ug = mysqli_fetch_array($result)) {
        echo $ug['underground_name'];
    }
} else {
    // Do something if no results were returned
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM