2016-05-15 77 views
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; 
    } 

我試圖

$r = $sth->fetch_all()(試圖分配和不分配一個返回值)都使用和不使用->bind_result() 但都失敗了。我究竟做錯了什麼?

回答

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

太謝謝你了。它總是一件小事,當你知道它是什麼時候......真的!再次感謝。 – user116032

+0

@ user116032很高興這有助於 – Ghost

+1

只是爲了關閉:http://stackoverflow.com/questions/1475701/how-to-know-if-mysqlnd-is-the-active-driver – user116032