PHP + MySQL - 而Loop不是

时间:2015-08-31 12:08:43

标签: php mysql

我是PHP + MySQL的新手所以,请原谅我的困惑 - 我不明白为什么我的代码不会遍历L3[i] = L2[i] + L3[i]列并回显所有行?

这是我的代码......

'underground_name'

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

  1. 尝试在SQL控制台上运行查询,并检查获得的结果。
  2. 尝试转储$ug数组以查看是否正在获取输出。
  3. 在初始化mysqli_fetch_*()时,您初次调用$row一次,在初始化$ug时再调用$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']; } 一次,并在while循环中再次调用test = [3, 5, 2] print [test[:i] + [v + 1] + test[i+1:] for i,v in enumerate(test)] 。这将错过前两行 - 因此,如果查询结果中只有两行,则不会打印任何输出。

  4. [[4, 5, 2], [3, 6, 2], [3, 5, 3]]
    

答案 1 :(得分:1)

您需要了解当您致电whilepublic void offlineEarnings() { SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); lastTime = sharedpreferences.getString("currentTime", currentTime); currentTime = String.valueOf(System.currentTimeMillis()); timeDifference = (Double.valueOf(currentTime)) - Double.valueOf(lastTime); timeDifferenceMinutes = (timeDifference / 1000) / 60; timeDifferenceSeconds = Double.valueOf(timeDifference) / 1000; $offlineMoneyEarned = ($employeeupgrade1level / 10) * timeDifferenceSeconds; $money = $money + $offlineMoneyEarned; if ($employerupgrade1level > 0) { $offlineEmployeesEarned = (Double.valueOf(Math.round(timeDifferenceSeconds) / $employercounter)); $employeeupgrade1level = $offlineEmployeesEarned + $employeeupgrade1level; } else $offlineEmployeesEarned = 0; if ($managerupgrade1level > 0) { $offlineEmployersEarned = (Double.valueOf(Math.round(timeDifferenceSeconds) / $managercounter)); $employerupgrade1level = $offlineEmployeesEarned + $employerupgrade1level; } else $offlineEmployersEarned = 0; drawEarnings(); } 时,您正在从结果堆栈中取出一行。因此,在您达到header.tpl声明之前,您已经关闭了两行。因此,如果您的查询返回了三个结果,那么您只需在循环中显示一个结果。

答案 2 :(得分:0)

尝试运行以下内容以查看返回的行数:

echo $ result-> num_rows()。'已返回行。' .PHP_EOL;

这里是您对代码的更新,我认为应该这样做(通过删除之前对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
}
?>