<?php
class CReturn {
const FAIL = false;
const SUCCESS = true;
public static function error($error_msg){
return array(self::FAIL, $error_msg);
}
public static function success($value){
return array(self::SUCCESS, $value);
}
}
class initDb {
private static $host = "localhost";
private static $user = "root";
private static $pass = "mysql";
private static $db = "db_test1";
public static function connect(){
$db_conn = mysql_pconnect(self::$host, self::$user, self::$pass) or die(mysql_error());
mysql_select_db(self::$db, $db_conn) or die(mysql_error());
mysql_query("SET NAMES UTF8;");
return $db_conn;
}
}
class DbAbstract {
protected $db_conn;
protected $table_name;
protected $jointable_name
public $_data;
public function __construct($db_conn){
$this->db_conn = $db_conn;
}
protected function _get($sql){
$this->_data = null;
$result = mysql_query($sql);
if(mysql_errno($this->db_conn) !== 0)
return CReturn::error("Mysql error [".mysql_errno($this->db_conn)."] : ".$sql."<br />".mysql_error($this->db_conn));
while($res = mysql_fetch_assoc($result)) {
$this->_data[] = $res;
}
return CReturn::success(mysql_num_rows($result));
}
protected function getTableName(){
return $this->table_name;
}
protected function getJoinTableName(){
return $this->jointable_name;
}
}
?>
classemployee.php
class DbEmployee extends DbAbstract {
protected $table_name = "t_employee";
protected $jointable_name = "t_position";
public function getEmployee(){ //ฟังก์ชั่นเช็คชื่อผู้ใช้
return $this->_get("SELECT * FROM {$this->getTableName()}
LEFT JOIN {$this->getJoinTableName()} ON {$this->getTableName()}.position_id = {$this->getJoinTableName()}.position_id");
}
public function searchEmployee($value){ //ฟังก์ชั่นเช็คชื่อผู้ใช้
return $this->_get("SELECT * FROM {$this->getTableName()}
LEFT JOIN {$this->getJoinTableName()} ON {$this->getTableName()}.position_id = {$this->getJoinTableName()}.position_id WHERE {$this->getTableName()}.emp_name LIKE '%$value%' OR {$this->getTableName()}.lastname LIKE '%$value%' OR {$this->getJoinTableName()}.position_name LIKE '%$value%'");
}
public function getEmployeeById($id){ //ฟังก์ชั่นเช็คชื่อผู้ใช้
return $this->_get("SELECT * FROM {$this->getTableName()} WHERE emp_id ='$id'");
}
}