0

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

在此先感谢,西蒙

4

1 回答 1

0

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);
?>
于 2015-06-23T16:36:57.760 回答