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;
}

出力には、最後の1行の1行のみが含まれます。すべてのレコードを取得するにはどうすればよいですか?

$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

値を直接割り当てるのではなく、viaに追加する必要があります。それ以外の場合は、ループの反復ごとに上書きします。$arr[]

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