Unable to connect to database: Access denied for u

2019-02-25 16:13发布

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","","");

6条回答
Deceive 欺骗
2楼-- · 2019-02-25 16:38

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

$con = mysql_connect("localhost","username","password");
查看更多
成全新的幸福
3楼-- · 2019-02-25 16:45

mysql_connect takes 3 parameters

$con = mysql_connect("localhost", "dbuser", "password");
查看更多
The star\"
4楼-- · 2019-02-25 16:49

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楼-- · 2019-02-25 16:51

Your default credentials are most likely:

$con = mysql_connect("localhost","root","");
查看更多
smile是对你的礼貌
6楼-- · 2019-02-25 16:51

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

$con = mysql_connect('localhost','root','');
查看更多
倾城 Initia
7楼-- · 2019-02-25 17:04

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");
查看更多
登录 后发表回答