Jump to: navigation, search

PHP mysql object oriented singleton class

From w3cyberlearnings

Contents

PHP Object Oriented and Singleton Class

In this part, you're going to learn how to design an object-oriented php concept by using singleton class for database connection. Please check the PHP mysql singleton class for additional detail about Singleton class.

Table

CREATE TABLE product 
(
	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	pname VARCHAR(200) NOT NULL,
	price DOUBLE NOT NULL,
	seller VARCHAR(200) NOT NULL
);

Class

  • Create the product class, and included four public variables: $id, $pname, $price, and $seller
  • Create two protected variables use for class connection and sql statement: $connect, and $sql
  • Initialize the singleton class to the protected variable: $connect.
  • In the destructor class, assign null to the connect variable.
<?php

require_once 'Singleton.php';

class product {

	public $id, $pname, $price, $seller;
	protected $connect;
	protected $sql;

	public function __construct() {
		$this->connect = Singleton::getconnect();
	}

	public function __destruct() {
		$this->connect = null;
	}
?>

product.php complete

<?php

require_once 'Singleton.php';

class product {

	public $id, $pname, $price, $seller;
	protected $connect;
	protected $sql;

	public function __construct() {
		$this->connect = Singleton::getconnect();
	}

	public function __destruct() {
		$this->connect = null;
	}

// this method insert_db to handle insert statement 

	public function insert_db() {
		$this->sql = 'INSERT INTO product (id, pname, price, seller) VALUES(NULL,"' .
				  addslashes($this->pname) . '",' .
				  $this->price . ',"' .
				  addslashes($this->seller) . '")';
		$return_id = $this->connect->mysql_execute($this->sql);

		if (!empty($return_id)) {
			return $return_id;
		} else {
			return false;
		}
	}
// update_db to handle the update of the product

	public function update_db() {
		if (!empty($this->id)) {
			$this->sql = 'UPDATE product SET pname="' .
					  addslashes($this->pname) . '",price=' .
					  $this->price . ',seller="' .
					  addslashes($this->seller) .
					  '" WHERE id=' . $this->id;

			$aff_rows = $this->connect->mysql_execute($this->sql);

			if (!empty($aff_rows)) {
				return $aff_rows;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

// to handle the delete of the product
	public function delete_db() {
		$this->sql = 'DELETE FROM product WHERE id=' . $this->id;
		$aff_rows = $this->connect->mysql_execute($this->sql);

		if (!empty($aff_rows)) {
			return $aff_rows;
		} else {
			return false;
		}
	}

// get or return all products
	public function get_products() {
		$this->sql = 'SELECT * FROM product';
		$return_arr = $this->connect->mysql_execute($this->sql);

		if (!empty($return_arr)) {
			return $return_arr;
		} else {
			return false;
		}
	}

// get product by id 
	public function get_product_by_id() {
		$this->sql = 'SELECT * FROM product WHERE id='.$this->id;
		$return_arr = $this->connect->mysql_execute($this->sql);

		if (!empty($return_arr)) {
			return $return_arr;
		} else {
			return false;
		}
	}

}

?>

Test 1: Insert Record

<?php

require_once 'product.php';

$myProduct = new product();
$myProduct->pname = "IPHONE 31";
$myProduct->price = 400;
$myProduct->seller = 'Alex mark';
$Id = $myProduct->insert_db();
echo $Id;
?>

Related Links

--PHP mysql singleton class-- PHP mysql object oriented singleton class--

Navigation
Web
SQL
MISC
References