mysql_real_escape_string () : 사용자 'root'@ 'localhost'에 대한 액세스가 거부되었습니다 (암호 사용 : NO).

크리스 엑스빌

이 오류가 내 오류 로그에서 반복되는 것을 발견했으며 그 원인으로 인해 메모리 누수가 내 사이트에 다운 타임이 발생한다고 생각합니다.

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

이러한 오류를 디버깅하는 데 도움을 주시면 감사하겠습니다 ....

심사 위원

여기서 문제는 연결이 함수의 범위를 벗어 났으므로 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);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관