Graphic Design shadow to enhance presentation of images.
Graphic Design shadow to enhance presentation of images.

Web Design - Tips, Tricks and Programmng

Creating a MySQL Database Connection

Return to Forum Home Page

Forum

Return to Topic

Return to Topic

Login to Forum

Login to Forum

Register to access Forum

Register to access Forum

Previous Thread Return to Topic Next Thread
Author Message

Norm Jurgen

Norm Jurgen

Date Joined:
Jan 19, 2010

Posts:
56

Posted: Tue Jul 27, 2010 09:12 AM

Subject: Creating a MySQL Database Connection

Here are two simple PHP functions to add to your libraries. Replace the localhost, user, password and database names with your information. Need this code in Object-oriented format? Just let us know and we can supply that, too.


// Create a connection to our database
function sql_connect() {

$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';

$conn = mysql_connect($dbhost,$dbuser,$dbpass);

if($conn) {
return $conn;
}else{
header('Location: 500.php');
}

}

// Perform our queries
function sql_query($query) {

$dbname = 'database';

$conn = sql_connect();
mysql_select_db($dbname, $conn);
$result = mysql_query($query, $conn);

if (!$result){
// The @ symbol supresses error messages from functions, use error supression in a live environment!
// @die("invalid query -- $query -- " . mysql_error());
header('Location: index.php');
}

return $result;
}

Return to Forum Home Page

Forum

Return to Topic

Return to Topic

Login to Forum

Login to Forum

Register to access Forum

Register to access Forum