0

I tried to fetch an array from my prepared statement using fetch_assoc() but PHP is throwing an error:

Call to undefined function fetch_assoc()

How can I retrieve an array from the prepared statement?

public function createUser($name, $email, $pass) {
    require_once 'PassHash.php';
    
    $api_key = uniqid('', true);
    $passwordHash = PassHash::hash($pass); // hash password
    
    $stmt = $this->conn->prepare("INSERT INTO `users`(`name`, `email`, `password`, `api_key`, `friends`)
                                                VALUES(?, ?, ?, ?, 0)");
    $stmt->bind_param("ssss", $name, $email, $passwordHash, $api_key);
    
    $result = $stmt->execute();
    $stmt->close();

    // check for successful store
    if ($result) {
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmtStore = $stmt->store_result();
        $user = fetch_assoc($stmtStore);
        $stmt->close();

        return $user;
    } else {
        return false;
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • `$user = fetch_assoc($stmtStore);` => `$user = $this->conn->fetch_assoc($stmtStore);` –  Dec 11 '16 at 11:08
  • @Hallur I've tried and I got this `Call to undefined method mysqli::fetch_assoc()` :( – Arthur Bachtiar Dec 11 '16 at 11:14
  • I was wrong, sorry... it would be `$stmt->fetch_assoc()` –  Dec 11 '16 at 12:42
  • i've tried it too, it became `Call to a member function fetch_assoc() on a non-object` – Arthur Bachtiar Dec 11 '16 at 12:52
  • did you make sure that there is no error in the query? http://stackoverflow.com/questions/5121027/fatal-error-call-to-a-member-function-fetch-assoc-on-a-non-object –  Dec 11 '16 at 12:53
  • yes, I'm sure. I can insert it into my database, but I can't select them – Arthur Bachtiar Dec 11 '16 at 13:49
  • Yes, that is correct, as otherwise, you wouldn't get into the if statement, where you try to use fetch_assoc. What happens if you change this line: `$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");` to `$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?") or die(mysqli_error($this->conn));` (just for debugging) –  Dec 11 '16 at 13:52
  • I still got this error `PHP Fatal error: Call to undefined method mysqli::fetch_assoc()` – Arthur Bachtiar Dec 11 '16 at 13:53
  • I start to wonder if it's because my hosting – Arthur Bachtiar Dec 11 '16 at 13:55
  • To me, this all seems so weird.. but you say it works on localhost, but not on your public host? what is the php version of the server??? (mysqli_fetch_assoc was added in version 5 and higher) –  Dec 11 '16 at 13:57

1 Answers1

0

fetch_assoc() is a method of the mysqli_result class, but nowhere in your code you have any object of this class.

First, you must fetch the result from the statement and then you can call a method on that result object. For example:

$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
Dharman
  • 30,962
  • 25
  • 85
  • 135