Login

new element designs - Web Design and Development - Winston Salem, NC 27107

Blog

MySQL PDO Class

PDO provides a good way to connect to different databases in PHP. This code example is for MySQL although I have a version for Oracle as well. If you want the Oracle version just comment and I will send it to you. Read on for the code.

class DB{

// database connection variables
private $db_user = 'USERNAME';
private $db_pass = 'PASSWORD';
private $db_host = 'localhost';

// PDO class var
private $DBH;
// PDO Statement class var
public $STH;

	// The singleton instance
	private static $DB_Instance;

	// Initiate connection to DB
	private function __construct(){

		try {
			// DB Connection
			$this->DBH = new PDO('mysql:host='.$this->db_host, $this->db_user, $this->db_pass);
			//$this->DBH->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

		} catch(PDOException $e) {
			// DB connection error message
			trigger_error("Could not connect to database: ". $e->getMessage(), E_USER_ERROR);
		}

	}
	public function __desctruct(){
		$this->DBH = null;
	}

	// Start singleton DB instance
	public static function open(){

        if (!self::$DB_Instance){
            self::$DB_Instance = new DB();
        }

        return self::$DB_Instance;
    }

	/* Prepare query SQL statement
	@param mixed $sql
	@return bool
	*/
	public function q($sql){

		if( !($this->STH = $this->DBH->prepare($sql)) ){

			$this->error($this->STH->errorCode(), $this->STH->errorInfo());

		} else {

			return true;	

		}

	}

	/* Bind variables with type
	@param string $bind
	@param mixed $var
	@param string $type
	@param int $len
	@return bool
	*/
	public function bind($bind, $var, $type, $len = null){

		switch ($type)
		{
			case 'STR':
					return $this->STH->bindParam($bind, $var, PDO::PARAM_STR, $len);
				break;

			case 'INT':
					return $this->STH->bindParam($bind, $var, PDO::PARAM_INT, $len);
				break;

			case 'LOB':
					return $this->STH->bindParam($bind, $var, PDO::PARAM_LOB);
				break;
		}

	}

	/* Execute the SQL statement
	@return bool
	*/
	public function run(){

		if(!$this->STH->execute()){
			$this->error($this->STH->errorCode(), $this->STH->errorInfo());
		} else {
			return true;
		}

	}

	/* Email error
	@param array $error
	*/
	private function error($errorCode, $errorInfo){

		$mssg .= 'Code: '.$errorCode."\n\r";
		$mssg .= $errorInfo[0].': '.$errorInfo[1]."\n\r";
		$mssg .= $errorInfo[2]."\n\r";

		mail("youremail@domain.com", "MySQL Error", "$mssg");

	}

} // END class DB

// EXAMPLE USAGE
/*
$DB = DB::open();

// Simple Query
$sql = "SELECT col1, col2 FROM table";
$DB->q($sql);
//$DB->bind(':var', $var, 'STR');
$DB->run();

while($row = $DB->STH->fetch()){

	echo $row['COL1'].' '.$row['COL2'].'';

}
*/

This is just a simple connection. I’m sure it can be improved. If you have any flavor you want to add, comment and I will consider your addition.

Tags: ,

Leave a Reply

Privacy/Security Policy | Terms and Conditions
Keywords: web design Greensboro Winston-Salem High Point Charlotte Flash multimedia photoshop web hosting NC north carolina
Listed in the North Carolina Web Designers Directory
Brainsites - the best webdesign directory
Logo, websites, and code © 2004-2010 new element designs