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
<?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);
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");
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.');
}
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
<?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);
Simon Hedberg wrote :1185
I`m also thinking of implementing something like this.. It was interesting to see your code.
I know however that the __construct function should be private since you should only be able to create instances from the getInstance method.
Check out http://www.phppatterns.com for more info on different patterns.
Joseph Crawford wrote :1186
very correct, the constructors for the Mysql and Sqlite should be private however the constructor for the database class should have been protected.
thanks for finding that error ;)
Joe
Joseph Crawford wrote :1187
i have also found out that you should make the classes final so that they cannot be extended such as