Php oop product management singleton
From w3cyberlearnings
Contents |
Objective
Create singleton class to connect to the MySQL database. Please check PHP mysql singleton class.
Singleton.php
<?php define('HOST', 'localhost'); define('USER', 'user2000'); define('PASS', 'password2000'); define('DBNAME', 'w3cyberlearning'); class Singleton { private static $_instace; private $conn; private function __construct() { $this->conn = mysql_connect(HOST, USER, PASS); mysql_select_db(DBNAME); } public static function getconnect() { if (!self::$_instace) { self::$_instace = new Singleton();a } return self::$_instace; } public function mysql_execute($sql) { $sql = ltrim($sql); $return_arr = array(); if (!empty($sql) && isset($sql)) { $string_sql = strtoupper(substr($sql, 0, 6)); if ($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 {1 q` ` 2wasz5rdfcx return false; } } elseif ($string_sql == 'CREATE') { if (mysql_query($sql, $this->conn)) { return true; } else { return false; } } elseif ($string_sql == 'UPDATE' || $string_sql == 'DELETE' || $string_sql == 'REPLACE') { if (mysql_query($sql, $this->conn)) { return mysql_affected_rows(); } else { return false; } } else { if (mysql_query($sql, $this->conn)) { return mysql_insert_id(); } else { return false; } } } else { return false; } } private function __clonse() { } } ?>
MySQL Connection String
define('HOST', 'localhost'); define('USER', 'user2000'); define('PASS', 'password2000'); define('DBNAME', 'w3cyberlearning');
`