colap Posted November 4, 2009 Share Posted November 4, 2009 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"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/180204-warning-mysql_connect-functionmysql-connect-access-denied/ Share on other sites More sharing options...
phpknight Posted November 4, 2009 Share Posted November 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180204-warning-mysql_connect-functionmysql-connect-access-denied/#findComment-951346 Share on other sites More sharing options...
waynew Posted November 5, 2009 Share Posted November 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180204-warning-mysql_connect-functionmysql-connect-access-denied/#findComment-951466 Share on other sites More sharing options...
waynew Posted November 5, 2009 Share Posted November 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180204-warning-mysql_connect-functionmysql-connect-access-denied/#findComment-951469 Share on other sites More sharing options...
phpknight Posted November 5, 2009 Share Posted November 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180204-warning-mysql_connect-functionmysql-connect-access-denied/#findComment-951476 Share on other sites More sharing options...
waynew Posted November 5, 2009 Share Posted November 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180204-warning-mysql_connect-functionmysql-connect-access-denied/#findComment-951478 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.