Php > Php Classes
PHP5 Database Objects Using Singleton Pattern
PHP5 Database Objects Using Singleton Pattern this code is by far complete, it contains a few functions to help you on your way. This shows a good example of how Sqlite is used. I have also shown how to extend classes (Database class is extended to both Sqlite and Myql classes although they could be stand alone) If you know PHP5 well please comment with any suggestions you come up with on how to make this code better. index.php // the following code will make it so that the class directory is in your include_path // this allows the __autoload function to work. $cur = ini_get("include_path"); $cur .= PATH_SEPARATOR.dirname(__FILE__).'\\include\\class\\';; ini_set("include_path", $cur); // this will load any classes that are not found and are located in the // /include/class/ folder. function __autoload($class) { include($class . '.php'); /* Check to see it the include defined the class */ if ( !class_exists($class, false) ) { trigger_error("Unable to load class $class", E_USER_ERROR); } } // get an instance of the sqlite database $db = Sqlite::getInstance('mysqlitedb', 0666); $db->Open(); $db->Create(); $result = $db->Query('SELECT * FROM states'); $arr = $db->FetchAll($result); echo '
'; print_r($arr); echo '
'; /* // this is how you would use the Mysql class $db1 = Mysql::getInstance('localhost', 'mydb', 'user', 'password'); $db1->Open(); $result = $db1->Query("SELECT * FROM table"); $arr1 = $db1->FetchArray($result); echo '
'; print_r($arr1); echo '
'; */ ?> /include/class/Database.php class Database { // an array of properties used by __get and __set private $props; // the actual connection resource protected $connection; // the hostname for the database server protected $hostname; // the name of the database to use protected $database; // the username to use to access the database protected $username; // the password to use to access the database protected $password; private function __construct($dbHost=null, $dbName=null, $dbUser=null, $dbPass=null) { $this->database = $dbName; $this->hostname = $dbHost; $this->username = $dbUser; $this->password = $dbPass; } protected function __set($name, $value) { if (isset($this->props[$name])) { $this->props[$name] = $value; } } protected function __get($name) { if (isset($this->props[$name])) { return $this->props[$name]; } else { return nulll; } } } ?> /include/class/Mysql.php class Mysql extends Database { static private $instance; public function __construct($dbHost=null, $dbName=null, $dbUser=null, $dbPass=null) { parent::__construct($dbHost, $dbName, $dbUser, $dbPass); } static function getInstance($dbHost, $dbName, $dbUser, $dbPass) { if(!Mysql::$instance) { Mysql::$instance = new Mysql($dbHost, $dbName, $dbUser, $dbPass); } return Mysql::$instance; } public function __set($name, $value) { if (isset($name) && isset($value)) { parent::__set($name, $value); } } public function __get($name) { if (isset($name)) { return parent::__get($name); } } public function Connected() { if (is_resource($this->connection)) { return true; } else { return false; } } public function AffectedRows() { return mysql_affected_rows($this->connection); } public function Open() { if (is_null($this->database)) die("MySQL database not selected"); if (is_null($this->hostname)) die("MySQL hostname not set"); $this->connection = @mysql_connect($this->hostname, $this->username, $this->password); if ($this->connection === false) die("Could not connect to database. Check your username and password then try again.\n"); if (!mysql_select_db($this->database, $this->connection)) { die("Could not select database"); } } public function Close() { mysql_close($this->connection); $this->connection = null; } public function Query($sql) { if ($this->connection === false) { die('No Database Connection Found.'); } $result = @mysql_query($sql,$this->connection); if ($result === false) { die(mysql_error()); } return $result; } public function FetchArray($result) { if ($this->connection === false) { die('No Database Connection Found.'); } $data = @mysql_fetch_array($result); if (!is_array($data)) { die(mysql_error()); } return $data; } } ?> /include/class/Sqlite.php class Sqlite extends Database { static private $instance; private $error; private $permission; private function __construct($dbName, $dbPerms) { $this->database = $dbName; $this->permission = $dbPerms; } static function getInstance($dbName=null, $dbPerms=null) { if(!Sqlite::$instance) { Sqlite::$instance = new Sqlite($dbName, $dbPerms); } return Sqlite::$instance; } public function Open() { if(is_null($this->database)) { die("Sqlite database not selected"); } if (is_null($this->permission)) { die("Sqlite permissions not set"); } if(file_exists($this->database)) { $this->connection = sqlite_open($this->database, $this->permission, $this->error); } else { $this->connection = sqlite_open($this->database, $this->permission, $this->error); $this->Query('CREATE TABLE states (state varchar(50))'); $this->Query("INSERT INTO states VALUES ('vermont')"); $this->Query("INSERT INTO states VALUES ('texas')"); } if ($this->connection === false) { die($this->error); } } public function Close() { sqlite_close($this->connection); $this->connection = null; } public function Query($sql) { if ($this->connection === false) { die('No Database Connection Found.'); } $result = sqlite_query($this->connection, $sql); if ($result === false) { die($sql); } return $result; } public function FetchArray($result) { if ($this->connection === false) { die('No Database Connection Found.'); } $data = @sqlite_fetch_array($result); if (!is_array($data)) { return false; } return $data; } public function FetchAll($result) { if ($this->connection === false) { die('No Database Connection Found.'); } $data = @sqlite_fetch_all($result); if (!is_array($data)) { $this->error = 'Fetch All Failed.'; return null; } return $data; } function table_exists($table) { return sqlite_fetch_single($rez) > 0; } } ?> index.php // the following code will make it so that the class directory is in your include_path // this allows the __autoload function to work. $cur = ini_get("include_path"); $cur .= PATH_SEPARATOR.dirname(__FILE__).'\\include\\class\\';; ini_set("include_path", $cur); // this will load any classes that are not found and are located in the // /include/class/ folder. function __autoload($class) { include($class . '.php'); /* Check to see it the include defined the class */ if ( !class_exists($class, false) ) { trigger_error("Unable to load class $class", E_USER_ERROR); } } // get an instance of the sqlite database $db = Sqlite::getInstance('mysqlitedb', 0666); $db->Open(); $result = $db->Query('SELECT * FROM states'); $arr = $db->FetchAll($result); echo '
'; print_r($arr); echo '
'; /* // this is how you would use the Mysql class $db1 = Mysql::getInstance('localhost', 'mydb', 'user', 'password'); $db1->Open(); $result = $db1->Query("SELECT * FROM table"); $arr1 = $db1->FetchArray($result); echo '
'; print_r($arr1); echo '
'; */ ?>
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage