PHP mysql singleton class
From w3cyberlearnings
Contents |
What is Singleton class?
In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The terms comes from the mathematical concept of a singleton.
Singleton.php class
This singleton class can handle insert, update, delete, replace, and create statement.
<?php // define the database connection string define('HOST', 'localhost'); define('USER', 'user2000'); define('PASS', 'password2000'); define('DBNAME', 'w3cyberlearning'); // start the single class class Singleton { // declare two private variables private static $_instace; private $conn; // declare private constructor class private function __construct() { $this->conn = mysql_connect(HOST, USER, PASS); mysql_select_db(DBNAME); } // create a singleton method public static function getconnect() { if (!self::$_instace) { self::$_instace = new Singleton(); } return self::$_instace; } // this method is used for update, delete, replace, select, and insert statement public function mysql_execute($sql) { $sql = ltrim($sql); $return_arr = array(); if (!empty($sql) && isset($sql)) { // Get the SQL first 6 characters of the SQL statement // UPDATE, DELETE, REPLACE, SELECT, and INSERT $string_sql = strtoupper(substr($sql, 0, 6)); // for create table if ($string_sql == 'CREATE') { if (mysql_query($sql, $this->conn)) { return true; } else { return false; } } // SELECT STATEMENT elseif ($string_sql == 'SELECT') { $statement = mysql_query($sql, $this->conn); while ($row = mysql_fetch_assoc($statement)) { $return_arr[] = $row; } if (count($return_arr) > 0) { return $return_arr; } else { return false; } // UPDATE, DELETE, and REPLACE STATMENT } elseif ($string_sql == 'UPDATE' || $string_sql == 'DELETE' || $string_sql == 'REPLACE') { if (mysql_query($sql, $this->conn)) { return mysql_affected_rows(); } else { return false; } // Expect INSERT statement } else { if (mysql_query($sql, $this->conn)) { return mysql_insert_id(); } else { return false; } } } else { return false; } } // stop or prevent clonse this class object private function __clonse() { } } ?>
Test 1:Create Table
<?php require_once 'singleton.php'; $dbh = Singleton::getconnect(); $id =0; $sql_table = "CREATE TABLE IF NOT EXISTS testtest( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(200) NOT NULL )"; $createdb = $dbh->mysql_execute($sql_table); if($createdb) { echo "Create table testtest"; } ?>
Output
Create table testtest
Test 2:Insert Record
<?php require_once 'singleton.php'; $dbh = Singleton::getconnect(); $insert = "INSERT INTO testtest(id,name) VALUES(null,'good'),(null,'excellent'),(null,'great')"; $dinsert = $dbh->mysql_execute($insert); if ($dinsert) { echo "Insert"; } else { echo 'failed'; } ?>
Test 3: Select Record
<?php require_once 'singleton.php'; $dbh = Singleton::getconnect(); print_r($dbh->mysql_execute("SELECT * FROM testtest")); ?>
Output
Array ( [0] => Array ( [id] => 1 [name] => good ) [1] => Array ( [id] => 2 [name] => excellent ) [2] => Array ( [id] => 3 [name] => great ) )
Test 4: Update Record
<?php $dbh = Singleton::getconnect(); print_r($dbh->mysql_execute("UPDATE testtest SET name='very good' WHERE id=1")); print_r($dbh->mysql_execute("SELECT * FROM testtest WHERE id=1")); ?>
Output
1Array ( [0] => Array ( [id] => 1 [name] => very good ) )
References
http://en.wikipedia.org/wiki/Singleton_pattern
Related Links
--PHP mysql singleton class-- PHP mysql object oriented singleton class--