Sunday, January 29, 2012

Fetch_array alternative for PHP prepared statements


UPDATE




$stmt = $this->db->prepare("
SELECT u.id, u.fname, u.lname, u.mname, u.type, u.email, u.salt,
u.pass, u.salt, u.approved, u.ban, u2.status
FROM `users` AS u
LEFT OUTER JOIN `log` AS u2
ON u2.user_id = u.id
WHERE u.email = ? LIMIT 1") or die($this->db->error);

$stmt->bind_param("s", $_POST['email']) or die($stmt->error);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 0) {
die($this->ajax->respond(7));
}
$result = $stmt->get_result();
$data = $result->fetch_array(MYSQLI_BOTH);



Trying to fetch array but getting following error for last line




Fatal error: Call to a member function fetch_array() on a non-object



Can't get it work. Please help

2 comments:

  1. With get_result() you can get a resultset from the executed statement:

    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_array(MYSQLI_BOTH))
    {

    ReplyDelete
  2. $data = $stmt->fetchAll();


    PDO gives this beautiful function for that purpose.

    Edit: i thought it was PDO interface. Why you aren't using pdo? I find it way comfortable than mysqli.

    ReplyDelete