Vicky
Vicky

Reputation: 41

MySQL Connection Error in PHP

I have set the password for root and grant all privileges for 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();



?>

Upvotes: 0

Views: 3145

Answers (4)

VolkerK
VolkerK

Reputation: 96189

The missing second underscore for __construct() in

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, ...)

When you create a new object via $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.
Now when you call $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:

resource mysql_query ( string $query [, resource $link_identifier ] )
[...]
If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

  • Fix the typo in _construct()
  • pass $this->connection to mysql_real_escape_string(), mysql_query() and mysql_error()
  • make sure open_connection() has all the parameters it needs, e.g. 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, e.g. 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.

Upvotes: 2

awgy
awgy

Reputation: 16934

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. You need to add the underscores to the variable names in your connect call.

Upvotes: 0

Michal Čihař
Michal Čihař

Reputation: 10091

You're connecting as SYSTEM user without password, which obviously is not allowed to connect to MySQL server.

Upvotes: 0

Yacoby
Yacoby

Reputation: 55465

You have not defined any of $DBSERVER,$DBUSER,$DBPASS within the current variable scope.

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.

Upvotes: 3

Related Questions