gpt4 book ai didi

php - 在 PHP 中一次获取多个 mysql 行

转载 作者:行者123 更新时间:2023-11-29 14:10:16 25 4
gpt4 key购买 nike

如何在 PHP 中一次从 MySQL 表中获取多行?

假设我的查询总共返回 5 行,并且我指定 block 大小为 2,则该函数必须连续返回包含 2+2+1 行的对象/数组。

附注我知道我可以手动迭代所有结果并自己执行相同的实现,但我正在寻找一些内置函数来以优化的方式干净地完成此操作。

最佳答案

考虑使用PDO如果您还没有使用它,请对象。这样,您就可以使用 fetchAll功能。

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

获取结果集中所有剩余的行:

Array
(
[0] => Array
(
[NAME] => pear
[0] => pear
[COLOUR] => green
[1] => green
)

[1] => Array
(
[NAME] => watermelon
[0] => watermelon
[COLOUR] => pink
[1] => pink
)

)

关于php - 在 PHP 中一次获取多个 mysql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610651/

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