tags:

views:

186

answers:

4

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



?>
+3  A: 

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.

Yacoby
A: 

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

Michal Čihař
THERE ARE JUST TWO LIKE ROOT@LOCALHOST AND 127,0...@LOCALHOST THERE IS NO SYSTEM SOMETHING...I HAVE SET THE PASSWORD FOR IT THOUGH
A: 

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.

awgy
ACTUALLY THERE'S NO PARAMATERS ERROR I TRIED JUST TO CHANGE IT. define('DB_SERVER','')? null : define("DB_SERVER","localhost");THIS IS IN CONFIG.PHP FILE I M CALLING USING INCLUDE("CONFIG");BUT I DONT KNOW IT DOESNT CALL THIS FILE. WHY I DONT KNOW...PLZ HELP
For a start, you'd need to do: include("config.php") not just include("config"). And you'll still need to follow Yacoby's advice, above.
James Burgess
Since there's obviously something wrong with your CapsLock key did you check the variables and passwords? They might be all in caps, too? (Sorry, this isn't helpful at all ..but I couldn't resist, really sorry)
VolkerK
i am sorry it's include("config.php") its type mistake
as i have tried putting user name ,paswwrod in MYSqlDatabase class it does work fine.but if i do it seperate it doesnt work
Might also want to add the absolute path for the included config: include("/path/to/file/config.php");
Phill Pafford
+1  A: 

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.
VolkerK
+1 and I'd give another +1 for "don't roll your own MySQL class" if I could.
chendral
thnx for ur help