Menu
MySQLi OOP CONNECTION

MySQLi OOP CONNECTION

 คนทำเว็บ  2017-10-25 21:06:28  4,329

PHP : MySQLi OOP CONNECTION

ถึงเวลาแล้ว เมื่อโลกเปลี่ยน เราก็ต้องเปลี่ยน เปลี่ยนจาก MySQL เป็น MySQLi กันครับ

 



class connectDB{
		private $host = "localhost";
		private $user = "db_username";
		private $pass = "db_password";	
		private $db = "db_name";  
		public $mysqli;

		public function __construct() {
				$this->db_connect();
		}
		
		private function db_connect(){
				$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->db);
						if (mysqli_connect_errno()){
								$message  = 'DB CONNECT ERROR : ' . mysqli_connect_errno();
								die($message);
						}
				mysqli_set_charset($this->mysqli,"utf8");
				return $this->mysqli;
		}
		
		public function return_sql($sql){
				$result = $this->mysqli->query($sql);
						if ($result){
								$arrData   = array();
								while ($rows = mysqli_fetch_array($result)) {$arrData[] = $rows;}
						}else{ 
								$message  = 'ชุดคำสั่งของ : ' . $sql . " ไม่ถูกต้อง";
								die($message);
						} 
				return $arrData;
				mysqli_close($this->mysqli);
		}
		
		
		public function record_count($sql){
				$result = $this->mysqli->query($sql);
				$num_rows = mysqli_num_rows($result);
				return $num_rows;
				mysqli_close($this->mysqli);
		}
		
		
		public function exe($sql){
				$result = $this->mysqli->query($sql);
				if (!$result) {
						$message  = 'ชุดคำสั่งของ : ' . $sql . " ไม่ถูกต้อง";
						die($message);
				}
				mysqli_close($this->mysqli);
		}
}




การเรียกใช้งาน


$conn = new connectDB;
$sSql = "INSERT table_po (po_name) VALUES ('รับทำเว็บ')";
$conn->exe($sSql);

$conn = new connectDB;
$sSql = "UPDATE table_po SET po_name = 'สยามโฟกัส' WHERE po_id=2";
$conn->exe($sSql);

$conn = new connectDB;
$sSql = "DELETE FROM table_po WHERE po_id=3";
$conn->exe($sSql);

$conn = new connectDB;
$sSql = "SELECT * FROM table_po";
$arrData = $conn->return_sql($sSql);
$recCount = $conn->record_count($sSql);
if($recCount>0){
	for ($sLoop=0;$sLoop<$recCount;$sLoop++){
		print $arrData[$sLoop][0] . "-" .  $arrData[$sLoop][1] . "
";
	}
}


มาเป็นเพื่อนกันครับ บนช่องทาง Social ด้านล่างนี้ครับ

--------------------------
► LINE@ : @siamfocus.com
► Facebook : fanpage.siamfocus
► Twitter (X) : siamfocus
► Linkedin : taam-siamfocus
► Instagram : iamtaam
► Youtube : SiAMFOCUS
► Tiktok : @taamsiamfocus
--------------------------

รับทำเว็บไซต์ สอนทำเว็บไซต์ รับดูแลเว็บไซต์

สายด่วนโทร 061-583-7888

บทความแนะนำ