thedigitalfeed.co.uk content
thedigitalfeed.co.uk/Code
Abstract your Database Connection
Posted on Wednesday the 25th of January, 2006
www.thedigitalfeed.co.uk/code/2006/01/25/abstract-your-database-connection
Obviously, the first thing you need in a dynamically-driven site is a database connection. You could use PHP's mysql_* functions, but this ties you to a MySQL database. A much better idea is to abstract common functions. If the site moves to a new database, these can be rewritten without altering the rest of your code.
All of my sites start off with an include('database.php'); line. This file is a class that handles all of the basic SQL functions. I'll go over them first, and then describe some methods of using them effectively. These functions were originally written by a colleague of mine, which I then wrapped in a class for integration with my own systems.
db_connect() is simple enough. It simply connects to a specific host with a user/pass combo, and selects a database to use. It returns $con as the identifier for the PHP connection - we'll use this later.
execute_sql() is another easy one. It simply runs a SQL query and returns a response if necessary. If you're just running a straightforward query (such as INSERT or UPDATE) you won't need the result. But if you're selecting records and need the recordset, you can assign the return to a variable.
num_results() simply counts the results in a recordset returned from execute_sql().
Finally, get_result() is used to grab the data in any specified field on any specified row.
These functions are wrapped in a class called db. At the end of the file, after the class, I declare the class like this:
Now, I have $con as a connection identifer that I can access from any function throughout the site. On the next page I'll go through how I use the functions for accessing any database information.
All of my sites start off with an include('database.php'); line. This file is a class that handles all of the basic SQL functions. I'll go over them first, and then describe some methods of using them effectively. These functions were originally written by a colleague of mine, which I then wrapped in a class for integration with my own systems.
db_connect()
function db_connect(){
// connects to the db server and selects a database
// provides $con as the database connection
$host = "localhost"; // host ip or name
$user = "username"; // username
$pass = "password"; // password
$db = "database"; // database to select
if( !$con = mysql_connect($host, $user, $pass) ){
return 0;
}
if( !mysql_select_db($db, $con) ){
return 0;
}else{
return $con;
}
}db_connect() is simple enough. It simply connects to a specific host with a user/pass combo, and selects a database to use. It returns $con as the identifier for the PHP connection - we'll use this later.
execute_sql($con,$sql)
function execute_sql( $con, $sql ){
// $con is a valid DB resource.
// $sql is the sql query to run.
// Success will return a valid result set
// Failure will return false
if( !$res = mysql_query($sql, $con) ){
return 0;
}else{
return $res;
}
}execute_sql() is another easy one. It simply runs a SQL query and returns a response if necessary. If you're just running a straightforward query (such as INSERT or UPDATE) you won't need the result. But if you're selecting records and need the recordset, you can assign the return to a variable.
num_results()
function num_results($res){
// $res is a valid resultset
// Returns the number of rows in $res
// On error will return false
return mysql_num_rows($res);
}num_results() simply counts the results in a recordset returned from execute_sql().
get_result()
function get_result( $res, $row, $field ){
// $res is a valid result set.
// $row is an integer value of the row which
// contains the result to be fetched.
// $field is the name of the field to be returned.
return mysql_result( $res, $row, $field );
}Finally, get_result() is used to grab the data in any specified field on any specified row.
These functions are wrapped in a class called db. At the end of the file, after the class, I declare the class like this:
$db = new db();
$con = $db->db_connect();
Now, I have $con as a connection identifer that I can access from any function throughout the site. On the next page I'll go through how I use the functions for accessing any database information.