October 10th, 2007 at

Connect to a MySQL database using php with error reporting

in: Scripts

This function allows you to connect to a MySQL database server and connect to the database in use.

This functionality has been covered many times on the web but i feel adequate error reporting has been left out of most connection scripts.

The function uses 4 inputs:

$hostname - usually "localhost" or the IP address of your database server
$username - the user-name setup for your database user
$password - the password setup for your database user
$database - the name of your database

This script can be used in conjunction with Create a file on your server using php script for better error logging.

 ######## start script

function mysql_conn($hostname, $username, $password, $database) {

  // connect to database server - if fail display message
  mysql_connect($hostname,$username,$password) or die("Sorry there has been a problem " . mysql_error());
  
  // connect to the database - if fail display message
  mysql_select_db($database) or die("Sorry there has been a problem " . mysql_error());

}

// sample use
mysql_conn($hostname, $username, $password, $database);

######## finish script

Download the complete php connection script here.

 

RSS feed for comments on this post | TrackBack URI