英文:
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 = "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
答案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['myColumn']
to get the value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论