douwei9759 2012-04-21 17:20
浏览 18
已采纳

在php中使用singleton数据库的mysql_real_escape_string?

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条回答 默认 最新

  • dtio35880438 2012-04-21 17:33
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用