PHP DBH->fetch() 和 fetchAll(),有没有办法摆脱返回数组中的"numbered"项?



我从一个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);
?>

相关内容

  • 没有找到相关文章

最新更新