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;
}
}
}
?>