I get the following error on my php pages that use database functions:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'apache'@'localhost'
I know I have to have a working connection to my database, but here is the 'problem'. The class that connects to my database looks like this:
class Database
{
private static $instance = null;
private static $conn;
private function __construct()
{
try {
self::$conn = new mysqli('localhost', 'root', 'user', 'database');
} catch (Exception $ex) {
$errorpager = new CustomPageGenerator();
$errorpager->generateErrorPage("Connection to database failed. Please try again later.");
}
}
public static function getInstance()
{
try {
if (self::$instance == null) {
self::$instance = new Database();
}
return self::$instance;
} catch (Exception $ex) {
$errorpager = new CustomPageGenerator();
$errorpager->generateErrorPage("Connection to database failed. Please try again later.");
return false;
}
}
public function query($sql)
{
try {
return self::$conn->query($sql);
} catch (Exception $ex) {
$errorpager = new CustomPageGenerator();
$errorpager->generateErrorPage("Connection to database failed. Please try again later.");
return false;
}
}
}
As you can see, I use the Singleton pattern for this. But even though I put a
Database::getInstance();
at the very beginning of my code to open up the connection I keep getting this error. How do I correctly open up the connection so that I can use the mysql_real_escape_string() function?
Thanks.
And here is le fix
Added this function to the Database class:
public static function getConnection()
{
self::getInstance();
return self::$conn;
}
Replaced every single mysql_real_escape_string( with mysqli_real_escape_string(Database::getConnection(),