PHP and MySQL Databases

A basic script for the handshake to connect to MySQL Database is…

define(‘DB_NAME’, ‘blahblah’);
define(‘DB_USER’, ‘blahblah’);
define(‘DB_PASSWORD’, ‘blahblah’);
define(‘DB_HOST’, ‘blahblah’);

$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if ($conn->connect_error){
die(“Connect FAILED!” . $conn->connect_error);

mysql_select_db(DB_NAME) or die(mysql_error());

echo “Connected successfully”;


The problem however is that if you run this you may get an “Warning: mysql_connect() [function.mysql-connect]: Access denied for user” with some kind of “Access denied for user ‘username’@’whateverIPaddress” error message. Presuming you entered the correct hostname, username, and password the secret to resolving this lies in the @symbol. For security reasons the database can only be accessed from particular ip addresses. If the file then is residing on your host that host may not be allowed to access that database. You have to setup the remote MySQL to allow access. By using a ping command towards your host you can resolve the IP address and find out what it is. Then you can setup you MYSQL server to allow access from that IP address. Tada. Fixed!

And if you want to access the database from a program like MySQL workbench you’ll need to do the same thing with your OWN IP address. Tada. Fixed!