PHP SQL 循环输出为 0 行

标签 php mysql sql database loops

我正在尝试创建一个 PHP 循环来向玩家展示他们正在参加的锦标赛。我在 phpMyAdmin 上尝试了该功能,并得到了我想要的结果。但是当我尝试在 PHP 上运行这个简单的脚本时,它输出为 0 行。

enter image description here

enter image description here

索引.php

chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");

if($mybb->user['uid']) {
    $sql = "SELECT * FROM players, tourneys WHERE players.forumname = 1 AND players.tid = tourneys.id";
    $result = mysqli_query($conn, $sql);
    // output data of each row
    if (mysqli_num_rows($result) > 0) {
        // output data of each ro
        while($row = mysqli_fetch_assoc($result)) {
            $name = $row['name'];
            $date = $row['date'];
            $time = $row['time'];
            echo $name;
            echo $date;
            echo $time;
        }
    } else {
        echo "0 rows";
    }
}

最佳答案

问题不在@Dagon 建议的“if ($mybb->user['uid']) {”中。尽量保留缩进良好的代码以避免这种混淆。运行下面的代码并发布数据库错误输出。

试试这个:

<?php
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");

if ($mybb->user['uid']) {

    $player_forumname = '1';

    // Return only the necessary fields
    $sql = "SELECT tourneys.name, tourneys.date, tourneys.time
            FROM tourneys
            INNER JOIN players ON players.tid = tourneys.id
            WHERE players.forumname = {$player_forumname}";

    $result = mysqli_query($conn, $sql);

    // If the query fails display the error
    if ($result === FALSE) {
        die('MySQLi Error: ' . mysqli_error($conn));
    }

    if (mysqli_num_rows($result) == 0) {
        die('0 rows');
    }

    // Returns only the associative array
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $name = $row['name'];
        $date = $row['date'];
        $time = $row['time'];

        echo 'Name:',$row['name'],'Date:',$row['date'],'Time:',$row['time'];
    }
}

关于PHP SQL 循环输出为 0 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28800598/

相关文章:

php - 使用 PHP 调用 URL 并获取 XML 响应

php - 从 Web 表单中搜索数据库表

php - 插入逆向工程 mysql 数据库

mysql - 是否可以在 SQL 中的 SELECT 中合并?

sql - postgres - SELECT/GROUP BY 中的部分列 - 列必须出现在 GROUP BY 子句中或用于聚合函数

android - Soundex/Metaphone for phonegap on android

PHP SoapClient 生成不同格式的 SOAP 请求

php - preg_替换引号内的所有换行符

java - 在服务端不添加跨源代码如何解决跨源问题?

mysql - 设置后外键可以为主键吗?