gpt4 book ai didi

php - 使 fetch_assoc 完全匹配 fetch_all

转载 作者:行者123 更新时间:2023-11-29 06:16:46 24 4
gpt4 key购买 nike

如何使用 fetch_assocfetch_all 的输出与循环精确匹配?我围绕 fetch_all 构建了代码,但我的 PHP 5.4 Bluehost 服务器不足以运行它(我正在与他们讨论)。这是我一直在使用但不起作用的方法:

public function getAllRecords($query) {
$results = array();
$r = $this->conn->query($query) or die($this->conn->error.__LINE__);
while ($row = $r->fetch_assoc()) {
$results[] = $row;
}

return $results;
}

编辑

此函数有效但只返回一个结果:

public function getOneRecord($query) {
$r = $this->conn->query($query.' LIMIT 1') or die($this->conn->error.__LINE__);
return $result = $r->fetch_assoc();
}

最佳答案

要使 $results 包含与 fetch_all 完全相同的输出,您可以使用此循环:

while ($row = $r->fetch_assoc()) {
$results[] = array_values($row);
}

或者只是在循环中使用fetch_row:

while ($row = $r->fetch_row()) {
$results[] = $row;
}

关于php - 使 fetch_assoc 完全匹配 fetch_all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35436667/

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