Unable to connect to database: Access denied for u

2019-02-25 16:02发布

问题:

I've seen a few errors like this, but I found no answer.

Unable to connect to database: Access denied for user ''@'localhost' to database 'socialdb'

socialdb is my database. The "Unable to connect to database:" part is located here.

$db = mysql_select_db("socialdb",$con);

if(!$db) {

    die ('Unable to connect to database: ' . mysql_error());

    }

I don't know what's causing this. Here are my mysql_connect details

<?php
$con = mysql_connect("localhost");

if(!$con) {

    die ('Error: ' . mysql_error());

    }

I need to find the root. Thanks.

I DON'T HAVE A USERNAME OR PASSWORD FOR MySQL

Should it be this?

mysql_connect("localhost","","");

回答1:

The error is pretty self-explanatory, you're not allowed to connect without specifying some credentials.

Change your call to mysql_connect() to something like this:

mysql_connect("localhost", "user", "password");


回答2:

Your default credentials are most likely:

$con = mysql_connect("localhost","root","");


回答3:

You're missing username and password parameter, should be:

$con = mysql_connect("localhost","username","password");


回答4:

You did not specify a user in neither mysql_connect or your PHP configuration.

Either set mysql.default_user and mysql.default_password in your PHP configuration or use the appropriate arguments for mysql_connect.



回答5:

mysql_connect takes 3 parameters

$con = mysql_connect("localhost", "dbuser", "password");


回答6:

even if you are not having a user. there exist 'root' user so use it

$con = mysql_connect('localhost','root','');