我正在使用这个语句来获取我的数据库中列中的元素
$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
但是我在尝试运行它时遇到了这个错误。
调用未定义的方法 mysqli_stmt::fetchAll();
是否需要向我的 php 文件添加一些内容以包含该方法或其他内容?
更新 1:
class LoginAPI
{
private $db;
function __construct()
{
$this->db = new mysqli('xxx', 'xxx', 'xxx', 'xxx');
$this->db->autocommit(FALSE);
}
function __destruct()
{
$this->db->close();
}
function login()
{
if(isset($_POST["lang"]))
{
$stmt = $this->db->prepare("SELECT code FROM locations");
$stmt->execute();
$stmt->bind_result($code);
//while($stmt->fetch())
//{
// result[] = "code"=>$code;
// break;
//}
$codeArr = array();
$result = $stmt->fetch_all();//|PDO::FETCH_GROUP;
sendResponse(200, json_encode($result));
return true;
}
sendResponse(400, 'Invalid Request');
return false;
}
}
最佳答案
您正在使用 mysqli,并且您得到的是语句而不是结果。如果你有一个 mysqli_result,你可以使用 fetch_all()
方法。
但是,由于这是一个 mysqli_stmt,您首先需要执行,然后在结果集上使用 fetch_all()
。看下面的例子
$stmt = $this->db->prepare("SELECT code FROM locations");
$stmt->execute();
//grab a result set
$resultSet = $stmt->get_result();
//pull all results as an associative array
$result = $resultSet->fetch_all();
注意:使用 fetch_all,您无需将结果绑定(bind)为原始代码。
关于php - 我如何在 php 中使用 fetchAll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13297094/