I'm writing a registration script for a website and tested the code with the function databaseConnection in the registration.php script, everyting worked but now I replaced the databaseConnection function to a seperate file so I dont have to type the same code again every time.
I took the instance from the database from the databaseConnection.php file and loaded it into the private variable $db_connection in registration.php, now I want to prepare the connection for a sql statement but the script returns error ' Call to undefined method DatabaseConnection::prepare()'
This is the databaseConnection.php script
<?php
class DatabaseConnection {
//private zodat niemand een nieuwe kan maken
private static $instance = null;
//constructor private zodat niemand instantie kan klonen
private function __construct(){
try{
$this->db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS);
return true;
} catch (PDOException $exception){
$this->errors[] = $this->lang['Database error'];
return false;
}
}
public static function getInstance(){
if(self::$instance === null)
self::$instance = new DatabaseConnection();
return self::$instance;
}
}
?>
I loaded the instance into $db_connection in my __construct function in registration.php like this
$this->db_connection = databaseConnection::getInstance();
Now my code crashes on the $check_user_name=... line
...
} else if ($this->db_connection != null){
$check_username_query = $this->db_connection->prepare('SELECT user_name, user_email FROM users WHERE user_name=:user_name OR user_email=:user_email');
...
Can someone help me solve this error? I read the similar questions on stackoverflow but couldn't figure it out with the information given in there.
Regards