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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值