【问题标题】:php: why is $row[0] not returning anything?php: 为什么 $row[0] 没有返回任何东西?
【发布时间】:2023-02-07 05:22:10
【问题描述】:

由于移动到新服务器,我正在转换我所有的 php 脚本。我很困惑为什么 $row[0] 不起作用。

我正确地将 $row 填充为一个数组,如果我在其上运行 foreach,我会很好地填充所有值。但是,如果相反,我尝试直接访问数组的第一个值作为 $row[0],我什么也得不到。有人知道吗?

$sql = "DESCRIBE USER";
$result = $dbh->query($sql);
$count=0;
while($row = $result->fetch_assoc()) {
        print $row[0]; // this prints nothing
        foreach($row as $column) {
            print "$column"; // this works as expected
        }
} #<-- while

【问题讨论】:

  • 因为它是 assoc 并且键是名称(列名)而不是数字索引。
  • 因为$row 是关联数组,而不是索引数组。你应该使用$row['column_name']
  • 如果您将 fetch_assoc() 更改为 fetch_row(),它将起作用。

标签: php mysql


【解决方案1】:

fetch_assoc 将您的结果作为列名和值的键值返回,因此您需要查看 $row['myColumn'] 以获取值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多