简体   繁体   English

PHP + MySQL-While循环不行

[英]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? 我是PHP + MySQL的新手,请原谅我的困惑—我不明白为什么我的代码不会循环通过'underground_name'列并回显所有行?

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. 尝试在SQL控制台上运行查询,并检查得到的结果。
  2. Try dumping the $ug array to see if the output is being fetched. 尝试转储$ug数组以查看是否正在获取输出。
  3. You're calling mysqli_fetch_*() thrice, once while initializing $row , once while initializing $ug , and once more in the while loop. 您要三次调用mysqli_fetch_*() ,一次在初始化$row ,一次在初始化$ug ,再在while循环中。 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); 您需要了解的是,当您调用mysqli_fetch_assoc($result); or mysqli_fetch_array($result); 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. 因此,在到达while语句之前,您已经关闭了两行。 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().' echo $ result-> num_rows()。 rows have been returned.'.PHP_EOL; 行已返回。'。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): 这是代码的更新,我认为应该可以解决(通过删除之前对fetch_assoc和fetch_array的调用,并添加一些基本检查来检查是否已返回任何行):

<?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
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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