Question : Connecting to MySQL via php for the first time

I just created my first MySQL database on my website. I created the db and created the appropriate tables.

When I try to connect to the db via php I get the error at the bottom.  I have the php and html files in www.mydomain.com/php
I created a user called quser and give it full priveleges. Here is my php code:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'quser'@'localhost' (using password: YES) in /home/sanderan/public_html/php/process-form.php on line 9

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'sanderan'@'localhost' (using password: NO) in /home/sanderan/public_html/php/process-form.php on line 10

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/sanderan/public_html/php/process-form.php on line 10

Warning: mysql_query() [function.mysql-query]: Access denied for user 'sanderan'@'localhost' (using password: NO) in /home/sanderan/public_html/php/process-form.php on line 15

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/sanderan/public_html/php/process-form.php on line 15

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/sanderan/public_html/php/process-form.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/sanderan/public_html/php/process-form.php:9) in /home/sanderan/public_html/php/process-form.php on line 21
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

           
Open in New Window Select All

Answer : Connecting to MySQL via php for the first time

Perhaps this is obvious, but even if the long list of errors seems daunting, don't sweat it too much.  Because it all starts with the fact that it can't connect to the database, then when it tries to run all the other DB functions they fail too, producing the errors.

To avoid the other errors, you may try:
$connect = mysql_connect("localhost", "username", "password") or die(mysql_error());

That will prevent your other errors from being displayed.

So, focus on:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'quser'@'localhost' (using password: YES) in /home/sanderan/public_html/php/process-form.php on line 9

You wrote your connection string correctly, but it seems that you are having trouble connecting.  Try the obvious (verify username & password EXACTLY as they appear on your server - with case sensitivity) - and then you might need to move to figuring out why 'localhost' isn't working.  Some servers have it set up a bit differently.

Depending on your hosting / server - and what back-end you used to set up tables & a db (PHPMyAdmin or something?) you should have some guidance as to how exactly to write your connection string.
Random Solutions  
 
programming4us programming4us