I am using this statement to fetch the elements in a column in my db
$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
but I get this error when trying to run it.
Call to undefined method mysqli_stmt::fetchAll();
Is there something I need to add to my php file to include that method or something?
UPDATE 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;
}
}
You're using mysqli, and you've got a statement not a result. If you have a mysqli_result you could be using the method fetch_all()
.
However, as this is a mysqli_stmt, you first need to execute and then use fetch_all()
on the result set. See below example
$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();
Note: with fetch_all you don't need to bind the result as your original code.