2014-07-10 265 views
-2

我發現這個錯誤在我的錯誤日誌中重複出現,我認爲它導致內存泄漏導致我的網站停機。mysql_real_escape_string():拒絕用戶'root'@'localhost'的訪問(使用密碼:否)

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

任何幫助t o調試這些錯誤是非常感謝....

+0

檢查'$ mysql_host','$ mysql_user','$ mysql_pass'的值是否正確...... – djidi

+0

聽起來你有一個連接問題。你確定允許用戶訪問SQL-Server並且也是適當的表嗎?而且用戶真的沒有密碼? – Thomas

+0

dbconnection的值都是正確的。網站正在運行,但我們說這些錯誤發生在某種程度上...... –

回答

1

看起來這裏的問題是連接超出了函數的範圍,所以mysql函數試圖創建一個沒有任何憑證的新連接。

嘗試將連接分配給一個變量,然後在函數中將其作爲全局調用。

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

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

您可以在這裏PHP手冊瞭解更多有關範圍http://www.php.net/manual/en/language.variables.scope.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); 
0

顯示錯誤可能被關閉在php.ini或您的apache配置文件。在頁面頂部

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

寫上面的腳本和u會看到錯誤消息:

您可以在腳本中打開它。 您應該在php錯誤日誌中看到相同的消息。

+0

因此,我的代碼沒有任何問題,該功能已棄用... –

+0

不贊成不應引起即時問題(只是一些信息消息)。 – Kickstart

+0

我只是打開它,並且即時看到這麼多的錯誤......注意:未定義的變量:等待注意:在...中使用未定義的常量......這些錯誤顯示一次,我設置爲顯示錯誤 –

相關問題