Jump to: navigation, search

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');

`

Related Links


  1. Chapter 1: Singleton class
  2. Chapter 2: Table
  3. Chapter 3: Product Class
  4. Chapter 4: Add Product
  5. Chapter 5: Activity Menu
  6. Chapter 6: List All Products
  7. Chapter 7: Product Detail
  8. Chapter 8: Update or Edit Product
  9. Chapter 9: Delete Product
Navigation
Web
SQL
MISC
References