Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:15:32+00:00 2026-06-02T06:15:32+00:00

I get the following error on my php pages that use database functions: Warning:

  • 0

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(),

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:15:35+00:00Added an answer on June 2, 2026 at 6:15 am

    I think the problem is that you are using mysql_real_escape_string().

    That is from a different library than mysqli, maybe that’s why there is no connection?

    From the mysql_real_escape_string page in the PHP manual:

    string mysql_real_escape_string ( string $unescaped_string [, resource
    $link_identifier = NULL ] )

    This is the definition for the second parameter, $link_identifier.

    The MySQL connection. If the link identifier is not specified, the
    last link opened by mysql_connect() is assumed. If no such link is
    found, it will try to create one as if mysql_connect() was called with
    no arguments. If no connection is found or established, an E_WARNING
    level error is generated.


    A possible, but kind of ugly solution, is to just add a wrapper to the real_escape_string method on your mysqli instance in your db singleton.

    public function escapeString($value){
        return $this->conn->real_escape_string($value);
    }
    

    Another alternative may be to use the procedural version for mysqli library (although, I haven’t actually tried to see if you can mix both, but I think you can): mysqli_real_escape_string

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following PHP code for trigger creation but always get error, please
I get following error when deploying on test server with II6 and Framework 3.5
I get following error when trying to access Xampp from a network I've tried
I get following error: 2012-04-04 23:46:18.374 istiqlaltv[17121:e903] -[istiqlaltvViewController moviePlayBackDidFinish]: unrecognized selector sent to instance
I am using the code as described in this question. However get following error
I get the following error: Client does not support authentication protocol requested by server;
I get the following error: Unhandled Exception: System.IO.IOException: The parameter is incorrect. at System.IO.__Error.WinIOError(Int32
I get the following error on firebug: this._processor is null and the error is
I get the following error in my flex code. Any ideas how to solve
I get the following error when attempting to connect to a web service via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.