简体   繁体   English

PHP中的MySQL连接错误

[英]MySQL Connection Error in PHP

I have set the password for root and grant all privileges for root. 我已经为root设置了密码,并为root授予了所有特权。 Why does it say it is denied? 为什么说它被拒绝?

****mysql_query() [function.mysql-query]: Access denied for user 'SYSTEM'@'localhost' (using password: NO) in C:\wamp\www\photo_gallery\includes\database.php  on line 56

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\photo_gallery\includes\database.php on line 56

The Query has problemAccess denied for user 'SYSTEM'@'localhost' (using password: NO)

Code as follows: 代码如下:

<?php
include("DB_Info.php");

class MySQLDatabase
{
    public $connection;
    function _construct()
    {
        $this->open_connection();
    }
    public function open_connection()
    {   
        $this->connection = mysql_connect($DB_SERVER,$DB_USER,$DB_PASS);
        if(!$this->connection)
        {
            die("Database Connection Failed" . mysql_error());
        }
        else
        {
            $db_select = mysql_select_db($DB_NAME,$this->connection);
            if(!$db_select)
            {
                die("Database Selection Failed" . mysql_error());
            }
        }
    }
    function mysql_prep($value)
    {
        if (get_magic_quotes_gpc())
        {
             $value = stripslashes($value);
        }
        // Quote if not a number
        if (!is_numeric($value))
        {
            $value = "'" . mysql_real_escape_string($value) . "'";
        }
        return $value;

    }
    public function close_connection()
    {
        if(isset($this->connection))
        {
            mysql_close($this->connection);
            unset($this->connection);
        }
    }
    public function query($sql)
    {

        $result = mysql_query($sql);
        $this->confirm_query($result);
        return $found_user;
    }
    private function confirm_query($result)
    {
        if(!$result)
        {
            die("The Query has problem" . mysql_error());
        }
    }

}

$database = new MySQLDatabase();



?>

You have not defined any of $DBSERVER , $DBUSER , $DBPASS within the current variable scope . 您尚未在当前变量scope中定义$DBSERVER$DBUSER$DBPASS中的任何一个。

If those variables are in the global scope you must add the following to the functions that uses them: 如果这些变量在全局范围内,则必须在使用它们的函数中添加以下内容:

global $DBSERVER,$DBUSER,$DBPASS;

If you intended to use the variables defined and then commented in the open_connection function you must first uncomment them and then alter the arguments passed to the mysql_connect function. 如果要使用定义的变量,然后在open_connection函数中添加注释,则必须首先取消注释,然后更改传递给mysql_connect函数的参数。

The missing second underscore for __construct() in 缺少__construct()的第二个下划线

class MySQLDatabase
{
    public $connection;
    function _construct()
    {

explains all the symptoms (mysql _query raising the warning, no undefined variable notices, no matter what you do to make the parameters available to function open_connection() it doesn't work simply because it's never called, access denied for system @localhost because your webserver runs as localsystem and therefore system is the default username for the default mysql connection, ...) 解释了所有症状(mysql _query发出警告,没有未定义的变量通知,无论您做什么使参数可用于function open_connection()它都无法工作,因为它从未被调用,对系统 @localhost的访问被拒绝,因为webserver作为localsystem运行,因此system是默认mysql连接的默认用户名,...)

When you create a new object via $database = new MySQLDatabase(); 通过$database = new MySQLDatabase();创建新对象时$database = new MySQLDatabase(); the method _construct() isn't invoked and therefore neither is $this->open_connection() and therefore no value is assigned to the property $connection which remains NULL and no connection to the mysql server is established . 该方法_construct()没有被调用,因此两者都不是$这- > OPEN_CONNECTION(), 因此没有值被分配给它仍然NULL并建立到MySQL服务器没有连接的属性$连接。
Now when you call $database->query('something'); 现在,当您调用$database->query('something'); and there is no database connection mysql_query() tries to establish the default connection as explained at http://docs.php.net/mysql_query : 并且没有数据库连接,mysql_query()尝试建立默认连接,如http://docs.php.net/mysql_query所述

resource mysql_query ( string $query [, resource $link_identifier ] ) 资源mysql_query(字符串$ query [,资源$ link_identifier])
[...] [...]
If the link identifier is not specified, the last link opened by mysql_connect() is assumed. 如果未指定链接标识符,则假定使用mysql_connect()打开的最后一个链接。 If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. 如果找不到这样的链接,它将尝试创建一个链接,就好像没有参数调用mysql_connect()一样。 If no connection is found or established, an E_WARNING level error is generated. 如果未找到或建立连接,则生成E_WARNING级错误。

  • Fix the typo in _construct() 修复_construct()中的错字
  • pass $this->connection to mysql_real_escape_string(), mysql_query() and mysql_error() 通过$ this-> connection到mysql_real_escape_string(),mysql_query()和mysql_error()
  • make sure open_connection() has all the parameters it needs, eg isset($DB_SERVER,$DB_USER,$DB_PASS,$DB_NAME) or die('missing parameters'); 确保open_connection()具有所需的所有参数,例如isset($DB_SERVER,$DB_USER,$DB_PASS,$DB_NAME) or die('missing parameters');
  • add some debug output to see whether it has been called at all, eg echo "Debug: this->connection = mysql_connect($DB_SERVER,$DB_USER,$DB_PASS);\\n"; 添加一些调试输出以查看是否已调用它,例如echo "Debug: this->connection = mysql_connect($DB_SERVER,$DB_USER,$DB_PASS);\\n";
  • don't roll your own MySQL class, use one of the gazillion existing classes, preferably one with broader acceptance. 不要滚动自己的MySQL类,请使用大量的现有类之一,最好是具有更广泛接受性的类。

您以没有密码的SYSTEM用户身份进行连接,显然,该用户不允许连接到MySQL服务器。

You really should clean up your questions. 您确实应该清理您的问题。 This is just a re-post of an earlier question you posted. 这只是您先前发布问题的重新发布。

I believe you have a typo in your code that may be causing your problem. 我相信您的代码中有错别字可能会导致您的问题。 You define your connection paramaters as $DB_SERVER and etc., but you're using $DBSERVER in your mysql_connect() call. 您将连接参数定义为$DB_SERVER等,但是在mysql_connect()调用中使用的是$DBSERVER You need to add the underscores to the variable names in your connect call. 您需要在连接调用中将下划线添加到变量名称中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM