mysql - > fetch_all()不起作用

时间:2016-05-15 23:03:07

标签: php mysql mysqli

请羞辱我。什么不对吗?我希望像 - > fetch_all(Opt)这样的内容,将所有结果放在一个数组中,但不能使它工作。这就是我最后做的事情:

   $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() 但都失败了。我做错了什么?

1 个答案:

答案 0 :(得分: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);