请羞辱我。这里有什么不对?我希望像 ->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()
但都失败了。我做错了什么?
最佳答案
首先,确保您的环境中有 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);
关于php - mysql -> fetch_all() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37244594/