mysql - PHP Fatal error: Call to a member function fetchAll() on a non-object in pdo login -


i've been working on web app client, , came across error when building database login code.

the config.php code

php try {     // create connection     $dbh = new pdo("mysql:host=" . $localhost . ";dbname=" . $database, $username, $dbpassword); } catch (pdoexception $e) {     // echo error message if connection fails     echo 'connection failed: ' . $e->getmessage();     exit; } 

and login.php code

require 'config.php'; $sql = "select * `users` `email` = ?"; $stmt = $dbh->prepare($sql); $result = $stmt->execute([$_post['email']]); $users = $result->fetchall();  if (isset($users[0])) {     if (password_verify($_post['password'], $users[0]->password)) {         // valid login         include 'dashboard.php';     }      else {         // invalid password         echo "<p class=''>the password entered incorrect. please try again.</p>";     } }  else {     // invalid email     echo "<p class=''>we're sorry, email not in our records. please check spelling , try again.</p>"; } 

the register.php works, , adds fields database, i'll add make sure i'm not missing something, since trying access database verify record.

require 'config.php'; require 'lib/password.php';  $company = $_post["company"]; $email = $_post["email"]; $password = $_post["password"]; $hash = password_hash($password, password_bcrypt);  $stmt = $dbh->prepare("insert users(id,company,email,password) values (?,?,?,?)"); $stmt->execute([$id, $company, $email, $hash]); 

you using wrong object($result) fetch

you need use $stmt this

$users = $stmt->fetchall(); 

also, need pass variable array before that

$stmt->execute(array($_post['email'])); 

http://php.net/manual/en/pdostatement.fetchall.php


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -