TMS
TMS

Reputation: 43

PHP MySQLi bind_result null?

I'm writing a class, but I stucked in a function. It requests the email address by username, but it's only returning null.

Code:

private function getUserEmail($username){
    if($stmt = $this->_mysqli->prepare("SELECT email FROM users WHERE username='?'")){
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->bind_result($email);
        $stmt->fetch();
        var_dump($email);
        $stmt->close();
        return $email;
    }
}

Upvotes: 2

Views: 1406

Answers (1)

Jaak Kütt
Jaak Kütt

Reputation: 2656

Try removing the single quotations around the ?

if($stmt = $this->_mysqli->prepare("SELECT email FROM users WHERE username=?")){

Otherwise it won't recognize the ? as a placeholder.

See documentation on syntax here: "The ? characters should not be enclosed within quotation marks, even if you intend to bind them to string values."

Upvotes: 1

Related Questions