mysql_real_escape_string():拒绝访问用户&root;' @' localhost' (使用密码:否)

时间:2014-07-10 08:54:03

标签: php mysql

我在错误日志中发现了这个错误,并且我认为它导致内存泄漏导致我的网站停机。

PHP Warning:  mysql_query(): Access denied for user 'root'@'localhost' (using password: NO) in /mysql.class.php on line 74

PHP Warning:  mysql_query(): A link to the server could not be established in /mysql.class.php on line 74

PHP Warning:  mysql_real_escape_string(): Access denied for user 'root'@'localhost' (using password: NO) in /functions.php on line 380

PHP Warning:  mysql_real_escape_string(): A link to the server could not be established in /functions.php on line 380

PHP Warning:  mysql_query(): Access denied for user 'root'@'localhost' (using password: NO) in /mysql.class.php on line 100

PHP Warning:  mysql_query(): A link to the server could not be established in /home/glo/mysql.class.php on line 100

在mysql.class.php中,这是第72到104行的代码

function &execute () {
    $query = $this->read();
    **$res = mysql_query($query);** // line 74

    if ($res || mysql_errno() == 1062) {
        return $res;
    }
    $mysql_error = mysql_error();
    $mysql_errno = mysql_errno();

    // If debug_backtrace() is available, we can find exactly where the query was called from
    if (function_exists("debug_backtrace")) {
        $bt = debug_backtrace();

        $i = 1;

        if ($bt[$i]["function"] == "SQL_Query_exec_cached" || $bt[$i]["function"] == "get_row_count_cached" || $bt[$i]["function"] == "get_row_count")
            $i++;

        $line = $bt[$i]["line"];
        $file = str_replace(getcwd().DIRECTORY_SEPARATOR, "", $bt[$i]["file"]);
        $msg = "Database Error in $file on line $line: $mysql_error. Query was: $query.";
    } else {
        $file = str_replace(getcwd().DIRECTORY_SEPARATOR, "", $_SERVER["SCRIPT_FILENAME"]);
        $msg = "Database Error in $file: $mysql_error. Query was: $query";
    }

    mysql_query("INSERT INTO `sqlerr` (`txt`, `time`) 
                 **VALUES (".sqlesc($msg).", '".get_date_time()."')");** // line 100

    if ( function_exists('show_error_msg') )
         show_error_msg("Database Error", "Database Error. Please report this to an Admin.", 1);
}

在functions.php中这里是代码

// MySQL escaping
function sqlesc($x) {
if (!is_numeric($x)) {
   $x = "'".mysql_real_escape_string($x)."'"; // Line 380
}
return $x;
}

我的连接代码是

function_exists("mysql_connect") or die("MySQL support not available.");

@mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die('DATABASE: mysql_connect: ' . mysql_error());
@mysql_select_db($mysql_db) or die('DATABASE: mysql_select_db: ' . mysql_error());

非常感谢任何调试这些错误的帮助....

2 个答案:

答案 0 :(得分:1)

看起来这里的问题是连接超出了函数的范围,所以mysql函数试图创建一个没有任何凭据的新连接。

尝试将连接分配给变量,然后在函数中将其称为全局变量。

$conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);

function &execute () {
    global $conn;
    $query = $this->read();
    $res = mysql_query($query, $conn); 
    ....

您可以在这里的http://www.php.net/manual/en/language.variables.scope.php

中阅读PHP手册中有关范围的更多信息

注意:不建议使用全局变量 - 最好使用更多OO方法并在MySQL类中创建连接,例如:

public function connect ($host, $user, $pass) {
    $this->connection = mysql_connect($host, $user, $pass);
}

function &execute () {
    $query = $this->read();
    $res = mysql_query($query, $this->connection); 
    ....

然后使用类似的东西连接

$db = new MysqlClassName();
$db->connect($mysql_host, $mysql_user, $mysql_pass);

答案 1 :(得分:0)

可以在php.ini或您的apache配置文件中关闭显示错误。

您可以在脚本中打开它:

   <?php 
          error_reporting(E_ALL);
          ini_set('display_errors', 1);
    ?>

在页面顶部写上面的脚本,你会看到错误信息。 您应该在php错误日志中看到相同的消息。