开发者

i am getting error like mysql_connect() acces denied for system@localhost(using password NO)

开发者 https://www.devze.com 2022-12-26 20:21 出处:网络
class MySQLDatabase { public $connection; function _construct() { $this->open_connection();} public function open_connection()

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());    }
        }}
          public function close_connection({  if(isset($this->connection)){
            mysql_close($this->connection); unset($this->connection)开发者_StackOverflow;}}
    public function query(/*$sql*/){ $sql = "SELECT*FROM users where id = 1";
        $result = mysql_query($sql); $this->confirm_query($result);
        //return $result;while( $found_user = mysql_fetch_assoc($result))
        {
         echo $found_user ['username'];
        }

    }
    private function confirm_query($result)
    {
        if(!$result)
        {
            die("The Query has problem" . mysql_error());
        }
    }

}

$database = new MySQLDatabase();
$database->open_connection();
$database->query();
$database->close_connection();

?>


I had the same issue in different forms. But two things to make sure - make sure you didn't close the connection before the query and make sure the link didn't go away. With the original mysql extension this sometimes happens. Testing for this can be done by "get_resource_type" - if the connection isn't of type mysql link, you need to reconnect.


Have you populated the DB_SERVER, _NAME, _USER, _PASS?


'Access denied' means you are connecting to the DB server properly, but the user/host combination you're connecting as (and from) does not have permission to access the server and/or specified database. You can check the permissions from within the mysql monitor with show grants for user@hostname (e.g. whatever your DB_USER and DB_PASS defines are).

Remember that MySQL's user system is completely seperate from the operating system's user system, so having a login account on the server does not automatically grant you an account in MySQL.

Full docs on the permissions granting system are here on dev.mysql.com.

edit:

You should NEVER connect to the server as root. Create a new database user with just enough privileges you need on whatever database(s) are required. Connecting as root is just asking for trouble, including possibly opening your server up to remote compromises.

Unless your web app requires modifying tables on-the-fly, something like this should be enough permissions for most everything to work just fine:

GRANT insert,select,update,delete on somedatabase.* TO username@localhost IDENTIFIED BY 'somepassword';

And then use DB_USER = 'username' and DB_PASS = 'somepassword' in your connection

That all being said, have you verified that DB_USER, DB_PASS, and DB_SERVER are properly set inside your class? Also note that for MySQL's purposes, combinations of user@host are considered distinct accounts. 'root@localhost' and 'root@127.0.01' may really be the same thing, but MySQL considers them utterly seperate and distinct.


Try using 127.0.0.1 instead of localhost. On certain MySQL drivers(particularly on OS X, IIRC), localhost actually refers to a socket connection instead of the local system.


In my configuration.php file on the line

public $dbtype = 'mysqli';

Just use "mysql" instead of "mysqli". It worked for me ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消