A simple MySQL DB abstration layer
By : Matt
<?php
/* A simple Mysql DB Abstraction Layer
by George McLachlan http://www.experimentalmonkey.co.uk/
------------------------------------------------
//Simply include this file on your page
require_once("DbConnect.class.php");
//Set up all yor paramaters for connection
$db = new DbConnect("localhost","user","password","database",$error_reporting=false,$persistent=false);
//Open the connection to your database
$db->open() or die($db->error());
//Query the database now the connection has been made
$db->query("SELECT * FROM....") or die($db->error());
//You have several options on ways of fetching the data
//as an example I shall use
while($row=$db->fetcharray()) {
//do some stuff
}
//close your connection
$db->close();
--------------------------------------------------
*/
Class DbConnect {
var $host = '';
var $user = '';
var $password = '';
var $database = '';
var $persistent = false;
var $conn = NULL;
var $result= false;
var $error_reporting = false;
/*constructor function this will run when we call the class */
function DbConnect ($host, $user, $password, $database, $error_reporting=true, $persistent=false) {
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->persistent = $persistent;
$this->error_reporting = $error_reporting;
}
function open() {
if ($this->persistent) {
$func = 'mysql_pconnect';
} else {
$func = 'mysql_connect';
}
/* Connect to the MySQl Server */
$this->conn = $func($this->host, $this->user, $this->password);
if (!$this->conn) {
return false;
}
/* Select the requested DB */
if (@!mysql_select_db($this->database, $this->conn)) {
return false;
}
return true;
}
/*close the connection */
function close() {
return (@mysql_close($this->conn));
}
/* report error if error_reporting set to true */
function error() {
if ($this->error_reporting) {
return (mysql_error()) ;
}
}
function query($sql) {
$this->result = @mysql_query($sql, $this->conn);
return($this->result != false);
}
function affectedrows() {
return(@mysql_affected_rows($this->conn));
}
function numrows() {
return(@mysql_num_rows($this->result));
}
function fetchobject() {
return(@mysql_fetch_object($this->result, MYSQL_ASSOC));
}
function fetcharray() {
return(mysql_fetch_array($this->result));
}
function fetchassoc() {
return(@mysql_fetch_assoc($this->result));
}
function freeresult() {
return(@mysql_free_result($this->result));
}
}
?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
More Database Code Articles More By Codewalkers
Please enable JavaScript to view the comments powered by Disqus. blog comments powered by