-1

I have got PHP script and MySQL database. I have following code inside:

mysql_connect("myserver.com", "mylogin", "mypass");
mysql_select_db("ggg");
$a = mysql_query("...");
echo mysql_error();

But it shows me:

Access denied for 'root'@'localhost' (USING PASSWORD : NO)

Why? In php.ini I haven't got specified any default settings. Help me please

peterm
  • 91,357
  • 15
  • 148
  • 157
TN888
  • 7,659
  • 9
  • 48
  • 84
  • 2
    your password is wrong dude – echo_Me Jan 30 '14 at 22:43
  • Have you created database 'ggg' and given permission for user 'mylogin' to access it with the password 'mypass'? – ashatch Jan 30 '14 at 22:43
  • @ashatch yes, i have. – TN888 Jan 30 '14 at 22:47
  • @echo_Me why do you mean? – TN888 Jan 30 '14 at 22:48
  • For what echo_Me means, see my answer, @Ty221 – miyasudokoro Jan 30 '14 at 22:50
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Jan 30 '14 at 22:50
  • 1
    Is that the _only_ code? It could for instance be caused by a `mysql_real_escape_string` call _before_ having a database. Also, Iǘe seen some problems with an anonymous user in the database: if you have a user in `mysql.user` with an empty username: delete it (and it may be the cause of the incorrect 'USING PASSWORD: NO' mention, its a bug in few versions of MySQL). – Wrikken Jan 30 '14 at 23:05

3 Answers3

2

Your user and password are empty strings, not whatever you think they are. How do I know this?

  1. "Using password : NO" means you didn't send a password at all. As you would know if you had googled your error message.

  2. 'root'@'localhost' are the usual default user when you supply nothing.


Plus, allow me to reiterate what chill0r already posted. I want to make sure you read this:

But that's not your main problem:

DO NOT use the mysql_* functions anymore. They are vulnerable for injection and are outdated. See http://www.php.net/manual/en/function.mysql-query.php for more informations.

Charlie
  • 11,380
  • 19
  • 83
  • 138
miyasudokoro
  • 1,705
  • 1
  • 15
  • 23
  • Ok, I know that... Your answer doesn't solve anything – TN888 Jan 30 '14 at 22:51
  • 1
    You "know that" you put in empty strings instead of the correct username and password? Then why are you asking us about it? Put in your actual username and password, and then it will work. – miyasudokoro Jan 30 '14 at 22:53
  • I know **what is causing that error**, I don't know how to solve it – TN888 Jan 30 '14 at 22:57
  • 1
    @Ty221 Yes you do. As spsc_tech said above; *"Put in your actual username and password, and then it will work"* – Phil Jan 30 '14 at 23:00
  • 2
    To solve this, look carefully at your username and password variables. Why are they empty? Is it because you saved this information under a different variable name, not the one you actually used? Is it because you never saved them at all? Is it because you included a file in the wrong place, or failed to include a file? – miyasudokoro Jan 30 '14 at 23:00
  • @Phil, I have already done that... It's first I've done while writing that code – TN888 Jan 30 '14 at 23:01
  • @Ty221 The error message says otherwise. Did you forget to upload the updates to your server? Are you executing the correct file? – Phil Jan 30 '14 at 23:02
  • @Phil Yes, I'm sure of that – TN888 Jan 30 '14 at 23:04
1

You never stored your connection. See the difference in the code below

$db = mysql_connect("myserver.com", "mylogin", "mypass");
mysql_select_db("ggg", $db);
$a = mysql_query("...");
echo mysql_error();

But that's not your main problem:

DO NOT use the mysql_* functions anymore. They are vulnerable for injection and are outdated. See http://www.php.net/manual/en/function.mysql-query.php for more informations.

chill0r
  • 1,127
  • 2
  • 10
  • 26
-3

your MySQL-settings deny access for user "root" without password. change the following to true in config.inc.php for your MySQL-settings:

$cfg['Servers'][$i]['AllowNoPassword'] = false; // change to true

hth

SaschaP
  • 883
  • 1
  • 7
  • 25
  • Ty only wants to do this if he wants to make his database extra easy to get into by anybody with an internet connection. – miyasudokoro Jan 30 '14 at 22:56
  • He want's to know why it's not working WITH a username and password. Not how to make his system insecure – chill0r Jan 31 '14 at 17:05