-3

I am trying to get started with MySQL. I installed the software this morning however whenever I try and view a MySQL page on the server I always get the following error:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /home/users/2014/21244429/public_html/IT Assignment 2B/cart.php on line 7
Access denied for user 'root'@'localhost' (using password: YES)

How can I get around this? Ive been searching for hours now to find a solution but nothing has helped. When installing MySQL I did set up a password and when I go into the installer this password is accepted when checking if root privileges.

Here is the code I am trying to display:

<?php

 session_start();

 $page = 'index.php';

 mysql_connect('localhost','root','password') or die(mysql_error());
 mysql_select_db('cart') or die(mysql_error());

 echo "Done!"

?>
Oscar
  • 511
  • 2
  • 10
  • 25
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Jan 09 '15 at 16:06
  • do you use linux? windows? 'root' and 'password' are just placeholders. you should change them to the real ones – Alex Jan 09 '15 at 16:07
  • @JayBlanchard even when using different files, I still get this error. I just want this to work so I can actually start working but I keep getting this error :( – Oscar Jan 09 '15 at 16:07
  • If you try logging in to your mysql server via the terminal, can you get in? Has your user been granted access to the 'cart' database? (I.e read/write privileges) – atmd Jan 09 '15 at 16:08
  • do this: mysql_connect('localhost','root',''); depending on what program you use, like xampp or wamp, root password is standard nothing, unless you've changed it –  Jan 09 '15 at 16:09
  • @atmd How can I do this? I am new to programming so please explain :( How can I check the user access also? – Oscar Jan 09 '15 at 16:09
  • @JamesPatterson `'localhost','root','password'` this is masking only for post question ? or this is actual code? if actual, so on `'root'` put your username & on `'password'` put password you put during installation – Hendra Nucleo Jan 09 '15 at 16:09
  • 2
    Either your credentials are wrong or you do not have access to the local MySQL server. – Peter Jan 09 '15 at 16:10
  • @Nucleo 1985 I am pretty sure he try to use them as real. He said he is beginner. Check the error message ;-) – Alex Jan 09 '15 at 16:11
  • what OS are you on?, Also see the other comments Re: your access details, is 'password' just a placeholder so you don't show your password OR is that what you are trying to log in with? – atmd Jan 09 '15 at 16:12
  • @Koen tried and all that changes is the "YES" in the error message is changed to "NO" :( – Oscar Jan 09 '15 at 16:13
  • @Nucleo1985 only the "password" is a placeholder. Should I change the 'root' to which username? – Oscar Jan 09 '15 at 16:14
  • @JamesPatterson of course, using your setup credential man. – Hendra Nucleo Jan 09 '15 at 16:15
  • @atmd password is a placeholder, I have put the real password in my code but no change. Should I be using the password I set up with MySQL this morning? Also I am running windows 7 bootcamp – Oscar Jan 09 '15 at 16:15
  • @Nucleo1985 thanks for your reply, how can i find what this username is? – Oscar Jan 09 '15 at 16:17
  • @JamesPatterson the one you fill during setup this morning! – Hendra Nucleo Jan 09 '15 at 16:19
  • @Nucleo1985 I dont recall being asked for a username in the MySQL installation, but I do remember being asked a password. How can I find the username out? – Oscar Jan 09 '15 at 16:22
  • @JamesPatterson So username is root(assuming you not setup any user yet) and password is your this morning filled password. Thats it – Hendra Nucleo Jan 09 '15 at 16:24
  • @Nucleo1985 but this is what Ive had all along and still I get the same error. This is making me so frustrated :( – Oscar Jan 09 '15 at 16:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68505/discussion-between-nucleo-1985-and-james-patterson). – Hendra Nucleo Jan 09 '15 at 16:26

1 Answers1

0
Like @Jay Blanchard said on comment don't use mysql as it deprecated and will be removed in future.

Here is very basic setup using mysqli:

$conn = mysqli_connect('localhost', 'myUsernameHereAndDontPutPlaceholderHere', 'myPasswordHereIfISetupAPassword', 'myDatabaseNameHereYesShouldBeMyDatabaseNameHere');

I don't want give mysql example as it not recomended anymore.

Hendra Nucleo
  • 591
  • 4
  • 18