2

我有以下代码。

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)
4

1 回答 1

3

您需要附加$arrvia 上[],而不是直接分配其值。否则,您将在每次循环迭代时覆盖它。

while ($stmt->fetch()) {
    $arr[] = array('question_title'=>$question_title,'question'=>$question);
    //-^^^^
}
于 2013-01-16T18:04:16.980 回答