$row[0] 为什么没有返回任何内容?

huangapple go评论59阅读模式
英文:

php: why is $row[0] not returning anything?

问题

我正在转换所有的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
英文:

I am converting all my php scripts due to moving to a new server. I am stumped as to why $row[0] is not working.

I am correctly getting $row populated as an array, and if I run a foreach on it, I get all the values populated just fine. But if, instead, I try to directly access the first value of the array as $row[0], I get nothing. Anyone know what?

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

答案1

得分: 0

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

英文:

fetch_assoc returns your results as a key value of column names and values, so you need to look at the $row[&#39;myColumn&#39;] to get the value.

huangapple
  • 本文由 发表于 2023年2月7日 05:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定