PHP Database Connection

Below is a simple php connection script with a query designed to show all the tables held within your database.

Copy and paste this into a php page, change the connection details to those specified and watch the database return the results.

You can continue to modify this script to run other quires on your database and return the results.

$database = 'enter your database name here';
$username = 'enter your username here';
$password = 'enter your password here';
$hostname = 'enter your hostname here';
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
    die('Not connected : ' . mysql_error());
// make foo the current db
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
    die ('Can\'t connect : ' . mysql_error());
// if you've got this far, the database connection has been made
echo "<p>You have successfully connected to $database</p>";
// quick script to display all the tables in your database
// run the mysql query and check the result
$result = mysql_query('show tables');
if (!$result) {
    die('Invalid query: ' . mysql_error());
// loop through the result and display
echo "<p>Tables within in $database</p>";
if(is_array(mysql_fetch_array($result))) {
	while ($row = mysql_fetch_array($result)) {
	    echo $row[0] . '<br>';
} else {
	echo "<p>No tables found</p>";