Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.