【问题标题】:Display more than one row from SQL SELECT从 SQL SELECT 中显示多行
【发布时间】:2015-03-07 16:10:35
【问题描述】:

我想显示图片中显示CID = 1 的所有行。 这是我的带有 SQL 的 PHP 代码: `

$contractCount = 1;

$sql = "SELECT categories.categoryID
        FROM categories
        LEFT JOIN link
        ON categories.categoryID = link.categoryID
        WHERE link.CID = '$contractCount'";

$res = $con->query($sql);
if (!$res) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}

while ($row = mysqli_fetch_array($res)) {
    echo $row['categoryID'];
}


这是显示 PHPMyAdmin 中名为 categories 的表的图像。

所以我需要输出为ITSM, Mar and HrAd,但我只得到ITSM 而不是其余的。

编辑 1: LEFT JOIN 在这里没有区别,链接表与 SELECT 语句无关

编辑 2: 我已经解决了问题是,我的错误是我在 SQL 查询中使用了错误的表名。

【问题讨论】:

  • 你应该使用准备好的语句。
  • categories 表中有多少条记录的 categoryID 与链接表中的 categoryID 匹配,并且它们的 CID 为 1?

标签: php mysqli phpmyadmin


【解决方案1】:

您需要使用函数mysql_fetch_row,这将获取一行并将指针移动到下一行。

while ($row = mysqli_fetch_row($res)) {
    echo $row['categoryID'];
}

【讨论】:

    猜你喜欢
    • 2010-11-27
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 2020-08-17
    • 1970-01-01
    • 2019-06-30
    相关资源
    最近更新 更多