Search code examples
phpmysqlmysql-error-1045

How to Connect to a Mysql database using PHP?


<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>

This is the code I am using to check if I am able to connect to Mysql.

I am having problem connecting using this code. Should I change localhost to the name of the website?

I tried ("www.abc.com","login username", "password") even this is not working.

EDIT:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mobile4_you'@'cl79.blahblah.com' (using password: YES) in /home/mobilew4/public_html/mysql.php on line 2 Access denied for user 'mobile4_you'@'cl79.blahblah.com' (using password: YES)


Solution

  • If you're using something like cPanel, it will prefix your username and your database with your username for cpanel, for example:

    > cPanel login: foo 
    > Database User: bar
    > Database: ey
    

    Your database user will be foo_bar and your database will be called foo_ey