gpt4 book ai didi

PHP DBH->fetch() 和 fetchAll(),有办法去掉返回数组中的 "numbered"项吗?

转载 作者:行者123 更新时间:2023-11-29 22:18:15 32 4
gpt4 key购买 nike

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

提前致谢,西蒙娜

最佳答案

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);
?>

关于PHP DBH->fetch() 和 fetchAll(),有办法去掉返回数组中的 "numbered"项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31008360/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com