Database.php

January 15, 2009 | Download
Tags: php, mysql

For my first real blog, I am going to show you a handy PHP script that makes a connection to a MySQL database. This is an, easy to understand, Object Oriented script. So it's good for all you who are just starting out and want to make sense of classes and objects.

Now, before I show you the code, I need to give credit to Jpmaster77 for his initial design of a PHP Login System. I highly recomend anyone trying to create a secure login system through PHP give that page a read.

/**
 * Database.php - A MySQL Connection Class 
 *
 * Originally written by Jpmaster77 @ http://evolt.org/user/62759
 * Orginal Script - http://evolt.org/PHP-Login-System-with-Admin-Features
 * 
 * Altered by Corey Hart @ http://www.codenothing.com/archives/blogs/database/
 * Altered on January 14, 2008
 */ 


class Database
{
	var $DB_SERVER  = "localhost"; //SERVER NAME (almost always localhost)
	var $DB_USER    = "myUSER"; //SERVER USERNAME
	var $DB_PASS    = "myPASS"; //SERVER PASSWORD
	var $DB_NAME    = "myDB"; //SERVER DATABASE
	var $connection; //connection to database

	/* Class Constructor */
	function Database(){
		// Make connection to the database
		$this->connection = mysql_connect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS) or die("Database Connection Error.");
		mysql_select_db($this->DB_NAME, $this->connection) or die("Cant select proper database.");
	}
	
	/**
	* Performs the given query and returns true, false, or 
	* the resource identifier
	*/
	function query($q){
		return mysql_query($q, $this->connection);
	}

	/**
	* Returns the hash of a single row in a MySQL database
	*/
	function hash($q){
		$result = $this->query($q);
		if (!$result || mysql_num_rows($result) != 1){
			return NULL;
		}
		return mysql_fetch_assoc($result);
	}
};

/* Initialize the database object */
$database = new Database;

I'm going to detail this one from the bottom up, as it will be easier to understand that way. So let's take a look at the hash function:

function hash($q){
	$result = $this->query($q);
	if (!$result || mysql_num_rows($result) != 1){
		return NULL;
	}
	return mysql_fetch_assoc($result);
}

This function will return a single row from the database, given the right query. If you send a query that returns multiple rows from the database, or doesn't select anything at all, a NULL will be returned to the variable that it is being set to.

function query($q){
	return mysql_query($q, $this->connection);
}

The query function does exactly what you think it does, performs the given query. For all non-'SELECT' queries, this function returns true or false depending on whether or not the query could be completed succesfully. For all 'SELECT' queries, this function will return the resource identifier for you to do with as you please. I will show more on what you can do with those in a later blog.

function Database(){
	// Make connection to the database
	$this->connection = mysql_connect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS) or die("Database Connection Error.");
	mysql_select_db($this->DB_NAME, $this->connection) or die("Cant select proper database.");
}

Ahhh, the meat of class. The first line of the function makes a connection to the database, and stores that connection in a class variable to be used throughout the rest of the script. The second line select's the database to be used for the script. As you can see, both lines have a fail safe for when either of them fail to do their job. If you would like to see the error that is causing these lines to fail, replace the die("error") with die(mysql_error()). I have removed that part for security reason's, as it shows details of database permissions.

var $DB_SERVER  = "localhost"; //SERVER NAME (almost always localhost)
var $DB_USER    = "myUSER"; //SERVER USERNAME
var $DB_PASS    = "myPASS"; //SERVER PASSWORD
var $DB_NAME    = "myDB"; //SERVER DATABASE
var $connection; //connection to database

What we have here are the class variables. The first four are database permission variables, in that they are used to log into the database with. Contact your systems administrator for these details, as only he will know your permissions. The $connection variable is the one that holds the connection to the database throughout the script.

class Database
{
	// Class Code ...
};

/* Initialize the database object */
$database = new Database;

Okay, so we are almost done. The first part of this section is what I call the Class Wrapper. This wraps all of the classes variables and functions into a single class that can be called on by an object. The second part sets the class into a variable('object'). This object can now be used to call functions or variables from within the 'Database' class. It's important to note that this is a very simplified verision of OOP PHP, and that this is not the norm for the PHP Community. If you would like to learn more about OOP PHP, check out it's page in the PHP Manual.

Download

Latest: database.zip
Released: January 15, 2009
Have a question or comment? ask@codenothing.com.