PHP Script to Test Database Connections
Need to test if a mySQL database has been setup, associated with a user and working properly? In most cases where you get a “can not connect to database” issue when installing a script, it is caused by something in your configuration files being wrong. To help you test, create a .php file (could be named anything, such as dbtest.php) and then put this inside of it:
<?php
$host = ‘localhost’;
$user = ‘mysqluser’;
$pass = ‘mypass’;
$db = ‘databasename’;
//Don’t change below here
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db, $conn);
echo ‘<hr />anything above this linebreak is BAD!’;
Make sure you change mysqluser, mypass, and databasename to the correct variables as you set them up in your hosting account control panel. Then, upload the test .php file into your public_html directory, and try to go to it via your browser. If you see no errors above the line break, it is connecting successfully. If you do see errors, then there is something wrong with the database or how you typed the information into the test script.
Remember also to use the
hostingusername_databaseusername
or
hostingusername_databasename
format (as it was told to you after creating the database or user for the database) when typing in your information. Kudos goes to the Joomla forum where I found this script.
Either way it should be a helpful tool when it comes to troubleshooting your MySQL database issues.


August 7th, 2009 at 12:11 am
Thanks for such a useful and timely post.
August 10th, 2009 at 4:46 am
Thanks for this, going to save it for later use. :D
August 20th, 2009 at 2:02 am
Great post and explanation! Connection strings are a huge pain point for a lot of people.