<?php $host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost $user = "example"; // your MySQL username $pass = "example"; // your MySQL password $db = "blakesdatabase"; // the database to which you're trying to connect to $result = mysql_query("SELECT * FROM blakesdatabase"); echo $result; ?> Code (markup): I am trying to connect to a database I set up. And I am having problems with this piece of code. I was wondering if someone could help me out. I am trying to echo out the first 2 rows in the database. I am getting this error - Warning: mysql_query(): Access denied for user 'wwwspike'@'localhost' (using password: NO) in /home/wwwspike/public_html/andulicsdesign.com/Blakes/Index.php on line 12 Warning: mysql_query(): A link to the server could not be established in /home/wwwspike/public_html/andulicsdesign.com/Blakes/Index.php on line 12 Code (markup): New to PHP Thank you
Cool, maybe it's not too late for you to forget everything you've learned and switch to a newer tutorial (or wherever else you got this code from). Seriously, don't continue learning where you're currently learning from. This code is very very old, very very slow, very very insecure, and very very deprecated, and you should not use it at all. Take a look at PDO: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers As for the error you're getting, it means that you're not connecting to the database to begin with. Take a look at the Connecting to MySQL section of the link above.
The issue is most likely that you aren't even attempting to connect to the database. In your example, you'd need to use mysql_connect() before running the query. I'll second the motion to use PDO. You really, really should.