วันจันทร์ที่ 18 สิงหาคม พ.ศ. 2557

PHP OOP - Code connect and basic function

Flie : config.php

<?php
if (preg_match("/config.php/i",$PHP_SELF)) {
//if(eregi("config.php",$_SERVER['PHP_SELF'])){

header("Location: ../index.php");
}


#--> config connect Mysql

define("DB_HOST","localhost");//define ค่าคงที่
define("DB_USER","root");
define("DB_PASS","1234");
define("DB_NAME","enmholding_db");

#--> Table
define("TB_HOME","tbl_home");
define("TB_PRODUCT","tbl_product");
define("TB_MEM","tbl_member");
define("TB_PROUDCT1","product");
define("TB_CATEGORY","category");
define("TB_BRAND_TH","brand_th");
define("TB_SUB_CATEGORY","sub_category");
?>

File : class_mysql.php

<?php
//if(eregi("class_mysql.php",$_SERVER['PHP_SELF'])){//eregi ไม่ว่าจะเขียนตัวเล็กหรือตัวใหญ่ก้อไม่สามารถเข้ามาได้
if (preg_match("/config.php/i",$PHP_SELF)) {
header("Location: ../index.php");

}

class DB{

var $host = DB_HOST;
var $conndb;
var $db;
var $result;
var $sql;

#--> Connect Database
function connectdb($db_name="database",$user="username",$pass="password"){

$this->username=$user;
$this->password=$pass;
$this->database=$db_name;

$this->conndb=mysql_pconnect($this->host,$this->username,$this->password) or die (mysql_error());
$this->db=mysql_select_db($this->database,$this->conndb)  or die (mysql_error());
mysql_query("SET NAMES UTF8");

//echo "OK"; //ทดสอบการใช้งานว่าใช้ได้หรือไม่

}

#--> Close Database
function closedb(){

mysql_close($this->conndb) or die (mysql_error());
}

#--> Query Database
function querydb($sql="sql"){

$this->sql=$sql;

if($result=mysql_query ($this->sql)){
return $result;
}else{
mysql_error();
return false;
}
}

#-->fetch
function fetch($sql="sql"){

if($result=mysql_fetch_array($sql)){
return $result;
}else{
mysql_error();
return false;
}
}

#-->rows
function row($sql){
return mysql_num_rows($sql);
}

#-->Add
function add($table="table",$data="data"){

$a_key=array_keys($data);
$v_valus=array_values($data);
$sum=count($a_key);

for($i=0; $i<$sum; $i++){

if(empty($add)){
$add="(";
}else{
$add=$add.",";
}
if(empty($val)){
$val="(";
}else{
$val=$val.",";
}

$add=$add.$a_key[$i];
$val=$val."'".$v_valus[$i]."'";
}

$add=$add.")";
$val=$val.")";

$sql=" INSERT INTO ".$table." ".$add." VALUES ".$val;

if(mysql_query($sql)){
return true;
}else{
mysql_error();
return false;
}

}

#--> UPDATE
function update($table,$daat,$where){
/*
$key=array_keys($data);
$valus=array_values($data);
$sum=count($key);
$set="";
*/
$key=array_keys($daat);
$value=array_values($daat);
$sum=count($key);
$set="";

for($i=0; $i<$sum; $i++){
if(!empty($set)){
$set=$set.",";
}
$set=$set.$key[$i]."='".$value[$i]."'";
}

$sql="UPDATE ".$table." SET ".$set." WHERE ".$where;
if(mysql_query($sql)){
return true;
}else{
mysql_error();
return false;
}
}

#--> Del
function del($table,$where){
$sql ="DELETE FROM ".$table." WHERE ".$where;
if(mysql_query($sql)){
return true;
}else{
mysql_error();
return false;
}
}

#--> Num
function num($field,$table,$where){

if($where==""){
$where="";
}else{
$where="WHERE ".$where;
}

$sql="SELECT $field FROM $table $where";

if($result=mysql_query($sql)){
return mysql_num_rows($result);
}else{
mysql_error();
return false;
}
}
}
?>




ไม่มีความคิดเห็น:

แสดงความคิดเห็น