0

Please,am learning how to code a simple PDO login system. One of my files, that is User.php has an error after rendering.

This is my code:

<?php

include_once('connection.php');
class User{

private $db;
public function _construct(){

$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function login($name,$pass){

if(!empty($name) && !empty($pass)){

    $stmt = $this->db->prepare(" SELECT * FROM users WHERE name = ? and pass = ? ");
    $stmt -> bindParam(1,$name);
    $stmt -> bindParam(2,$pass);
    $stmt -> execute();

    if($stmt->rowCount()==1){
        echo "successfully login";
    }else{
        echo "incorrect username or password";
    }
    } else{
        echo "enter correct username and password";
    }

Here is the error:

Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\simplelogin\User.php on line 16

Ivan Barayev
  • 2,035
  • 5
  • 24
  • 30
Emmanuel
  • 7
  • 3
  • Possible duplicate of [Fatal error Call to a member function prepare() on null](http://stackoverflow.com/questions/30627027/fatal-error-call-to-a-member-function-prepare-on-null) – hjpotter92 Jun 21 '16 at 09:16

1 Answers1

0

This means $this->db is null and has not been initialized.

Change this line: public function _construct(){

To:

public function __construct(){

You are missing an underscore in the constructor. Hope this helps.

Osama Sayed
  • 1,993
  • 15
  • 15
  • Thanks @ Osama.Please will like to know how to hash the password – Emmanuel Jun 21 '16 at 09:40
  • @Emmanuel You can use password_hash() function http://php.net/manual/en/function.password-hash.php To hash your password `$hashedPassword = password_hash("rasmuslerdorf", PASSWORD_DEFAULT);` and you can use password_verify function http://php.net/manual/en/function.password-verify.php to verify the password against its hashed value. Check this example here http://php.net/manual/en/function.password-verify.php#refsect1-function.password-verify-examples. Hope this help. – Osama Sayed Jun 21 '16 at 09:53
  • Man,my problem i dont understand the code.Aside,finding it difficult how to insert it in my code. – Emmanuel Jun 21 '16 at 10:13