Jump to content

Warning: mysql_connect() [function.mysql-connect]: Access denied


colap

Recommended Posts

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: NO) in a.php on line 10
Could not connect: Access denied for user 'www-data'@'localhost' (using password: NO)

 

<?php
class cl_Database {

    private $v_host="localhost";
    private $v_user="root";
    private $v_password="aaaaaa";
    private $v_database_name="m";

    public function fun_connect() {
        $con=mysql_connect("$v_host","$v_user","$v_password")or die('Could not connect: ' . mysql_error());
        mysql_select_db("$v_database_name");
    }

}
?>

Get rid of the class and variables and hardcode it.  I think it might have something to do with that weird hostname because it is not even using your password.  Right there, it is a red flag.  Email your hosting company to make sure you are getting the hostname right. 

Please do continue on your way towards OO programming. You only made one simple mistake:

 

<?php
class cl_Database {

    private $v_host="localhost";
    private $v_user="root";
    private $v_password="aaaaaa";
    private $v_database_name="m";

    public function fun_connect() {
        $con=mysql_connect($this->v_host,$this->v_user,$this->v_password)or die('Could not connect: ' . mysql_error());
        mysql_select_db($this->v_database_name);
    }

}
?>

 

When referencing class variables from within a class, always use $this->variable_name. $this refers to THIS instance.

Get rid of the class and variables and hardcode it.  I think it might have something to do with that weird hostname because it is not even using your password.  Right there, it is a red flag.  Email your hosting company to make sure you are getting the hostname right. 

 

Firstly, "localhost" is one of the most commonly used hostnames. Secondly, you shouldn't be telling people to stay away from OO programming.

I meant for debugging purposes to solve the issue not to get rid of classes altogether.  I wasn't talking about localhost, but the www-dat part.  That being said, for this, you could just use this: http://php.net/manual/en/book.mysqli.php.  No sense in reinventing the wheel for something there is already a class for.

I think he made a blunder. I'm guessing he changed his username in the code while forgetting to change it in the script he quoted. Either way, it wasn't classes that caused the problem (although he wasn't using classes properly). And I use a db class. It's for connection details. It also helps me insert data quicker and perform other queries much more easily.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.