0

I'm trying to make a login in PHP and PDO but I get

Fatal error: Call to a member function prepare() on null in DBOperations.php on line 23

the whole time. I've been looking for a solution on the internet, but I only found some errors I didn't have, like Fatal error Call to a member function prepare() on null. Can someone please help me?

Constants.php:

<?php

define('DB_NAME', 'roedel');
define('DB_USER', 'root');
define('DB_HOST', 'localhost');
define('DB_PASS', '');

?>

DBConnect.php:

<?php

class DBConnect {

    private $con;

    function __construct() {

    }

    function connect() {

        require_once dirname(__FILE__).'/Constants.php';

        try{

            $this->con = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);

        } catch (Exception $ex) {

            echo "No connection. Try again!" . $ex;

        }



    }    
}

?>

DBOperations.php:

<?php

class DBOperations {

    private $con;

    function __construct() {

        require_once dirname(__FILE__).'/DBConnect.php';

        $db = new DBConnect();

        $this->con = $db->connect();

    }

    function createUser($name, $pass, $email){

        $password = md5($pass);

        $rank = "lid";

        $stmt = $this->con->prepare("INSERT INTO 'users' ('id', 'name', 'password', 'email', 'rank') VALUES (NULL, ?, ?, ?, ?)");
        $stmt->bindParam(1, $name);
        $stmt->bindParam(2, $password);
        $stmt->bindParam(3, $email);
        $stmt->bindParam(4, $rank);

        if ($stmt->execute()){

            return true;

        }else{

            return false;

        }


    }

}

?>
Community
  • 1
  • 1
JvdB100
  • 1
  • 1
  • 1
  • 1
    It must have failed connection to mysql. Are u sure you don't see `"No connection. Try again!" . $ex;`? Try by letting the exception raise inside the méthod `createUser` to be sure – smarber Jan 24 '17 at 16:25

1 Answers1

0

It means that the variable (ie DBOperations::con here) is NULL.

Look at your code more carefully here :

$this->con = $db->connect();

Here, you expect DbConnect::connect() to return the connection but it doesn't return anything (in fact, it "returns" null).

Your missing the instruction return $this->con; in DBConnect::connect()

Moreover, you should'nt set your require inside __construct() (or any other function) but at head of your file. ;)

EDIT : Instead of :

if ($stmt->execute()){
    return true;
}else{
    return false;
}

You can write :

return $stmt->execute();

Because it returns true or false already. ;)

zenko
  • 147
  • 1
  • 8
  • Thanks a lot for your help!!! Now I don't get the error, but return $stmt->execute(); returns false. Can you help me with my new problem? – JvdB100 Jan 24 '17 at 17:09
  • You got 2 options : [Exception](http://php.net/manual/fr/pdo.setattribute.php) with `setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` or [Manual error management](http://php.net/manual/fr/pdostatement.errorinfo.php) – zenko Jan 25 '17 at 10:30