2016-05-15 2 views
1

私を恥ずかしくしてください。ここには何がないのですか? 1つのライナーである - > fetch_all(Opt)のようなものですべての結果を配列に入れることを望んでいましたが、動作させることはできませんでした。 mysql - > fetch_all()が動作しない

$s = "select id, username from users"; 
    $conn = db_connect(); 
    $sth = $conn->prepare($s); 
    $sth->execute(); 
    $sth->bind_result($id, $un); 
    $ida = array(); 
    while ($sth->fetch()) { 
    $ida[] = $id; 
    } 

が、私は両方使って ->bind_result() を使用しますが、両方に障害が発生していない(割り当て、戻り値を割り当てない試してみました)

$r = $sth->fetch_all()

を試してみました:これは私がやって巻き取るものです。私は間違って何をしていますか?

答えて

2

まず、ご使用の環境にmysqlndが設定されていることを確認してください。

次に、->fetch_all()を使用するには、まず->get_result()メソッドを使用する必要があります。

ここシーケンスです:

$s = "select id, username from users"; 
$conn = db_connect(); 
$sth = $conn->prepare($s); 
$sth->execute(); 
$data = $sth->get_result(); // get result first 
$result = $data->fetch_all(MYSQLI_ASSOC); // then fetch all 
print_r($result); 
+0

はありがとうございました。それは常に小さなものです。あなたがそれが何であるかを知ると、... doh!再度、感謝します。 – user116032

+0

@ user116032確かに嬉しい助けを受けた – Ghost

+1

閉鎖のためだけに:http://stackoverflow.com/questions/1475701/how-to-know-if-mysqlnd-is-the-active-driver – user116032