2016-05-15 82 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