我从一个PHP文档页面复制并粘贴示例,因为情况类似:当我使用DBH->fetch()执行MySQL查询时,我获得了一个数组:
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:n");
$result = $sth->fetchAll();
print_r($result);
?>
输出为:
Fetch all of the remaining rows in the result set:
Array
(
[0] => Array
(
[name] => pear
[0] => pear
[colour] => green
[1] => green
)
[1] => Array
(
[name] => watermelon
[0] => watermelon
[colour] => pink
[1] => pink
)
)
有没有一种方法可以告诉驱动程序只返回"命名"的数组元素,并删除带有数字索引的数组元素?类似于:
Fetch all of the remaining rows in the result set:
Array
(
[0] => Array
(
[name] => pear
[colour] => green
)
[1] => Array
(
[name] => watermelon
[colour] => pink
)
)
提前感谢,Simone
Fetch_Assoc仅返回命名数组。这是您的代码和零钱。
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:n");
$result = $sth->fetch_assoc();
print_r($result);
?>