gpt4 book ai didi

php - 从 mysqli 语句结果返回关联数组

转载 作者:行者123 更新时间:2023-12-04 05:15:11 25 4
gpt4 key购买 nike

我有以下代码。

public function fetch_questions($count=null)
{
$stmt = $this->mysqli->prepare("SELECT question,question_title FROM questions order by id");
$stmt->execute();

$stmt->bind_result($question,$question_title);

$arr = array();
while ($stmt->fetch()) {
$arr = array('question_title'=>$question_title,'question'=>$question);
}
$stmt->close();

return $arr;
}

输出仅包含一行,即最后一行。如何检索所有记录?
$questions = $db->fetch_questions();
var_dump($questions);

var_dump 的输出:
    array
'question_title' => string 'aaaaaa' (length=6)
'question' => string 'aaaaaaaaaaaaaaaaaaaa' (length=20)

最佳答案

您需要附加到 $arr通过 [] ,不直接赋值。否则,您将在每次循环迭代中覆盖它。

while ($stmt->fetch()) {
$arr[] = array('question_title'=>$question_title,'question'=>$question);
//-^^^^
}

关于php - 从 mysqli 语句结果返回关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14364075/

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