| Server IP : 104.21.80.248 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/myschool/benjama/includes/ |
Upload File : |
<?
/*
ชื่อไฟล์ class.mysql.php
การใช้งาน ใช้ในการเชื่อมต่อฐานข้อมูล MySQL
ผู้เขียน อัษฎา อินต๊ะ
ติดต่อ [email protected]
*/
if (preg_match("/class.mysql.php/",$_SERVER['PHP_SELF'])) {
Header("Location: ../index.php");
die();
}
class DB{
//ส่วนของการเชื่อมต่อ
var $host = DB_HOST ;
var $database ;
var $connect_db ;
var $selectdb ;
var $db ;
var $connect = 0 ;
var $sql ;
var $table ;
var $where;
var $error ;
var $datanumtype = array('bit','tinyint','bool','boolean','smallint','mediumint','int','integer','bigint','float','double','decimal','dec');
////////////////////// ฟังก์ชั่นต่างๆ //////////////////////
//เชื่อมต่อดาต้าเบส
function connectdb($db_name="database",$user="username",$pwd="password"){
$this->database = $db_name;
$this->username = $user;
$this->password = "Ben@007";
if(! $this->connect){
//echo $this->host ."-" . $this->username ."-" . $this->password ."-" . $this->database ."<br>";
$this->connect_db = mysqli_connect ( $this->host, $this->username, $this->password,$this->database) or $this->_error();
//$this->connect_db = mysql_pconnect ( $this->host, $this->username, $this->password ) or $this->_error();
//$this->db = mysqli_select_db ( $this->connect_db ,$this->database ) or $this->_error();
$this->connect =1;
}
mysqli_query($this->connect_db, "SET NAMES UTF8");
mysqli_query($this->connect_db, "SET character_set_results=utf8");
return true;
}
//ปิดการเชื่อมต่อดาต้าเบส
function closedb( ){
mysqli_close ( $this->connect_db ) or $this->_error();
$this->connect =0;
}
function real_string($txt){
return mysqli_real_escape_string($this->connect_db , $txt);
}
// get column
function array_column($table="table"){
$sql = "SHOW COLUMNS FROM ".$table." WHERE Extra = ''";
if ($res=mysqli_query($this->connect_db, $sql)){
while($row = mysqli_fetch_assoc($res)){
$arr[$row['Field']] = $row ;
}
return $arr;
}else{
$this->_error($sql);
return false;
}
}
//ตรวจสอบว่ามีข้อมูลหรือไม่ ถ้าไม่มี add ถ้ามี update
//$db->update_add("table",array("field"=>"value"), "a=b" );
function update_add($table="table",$data="data",$where=""){
$nrow = $this->num_rows($table,"*",$where) ;
if($nrow==0){
$this->add_db($table , $data );
}
else{
$this->update_db($table,$data,$where);
}
}
//เพิ่มข้อมูล
//$db->add_db("table",array("field"=>"value"));
function add_db($table="table", $data="data"){
$key = array_keys($data);
$value = array_values($data);
$sumdata = count($key);
$collist = $this->array_column($table) ;
$column = array_keys($collist );
foreach ($collist as $kkk => $vvv) {
$aaa = explode('(', $collist[$kkk]['Type']);
$collist[$kkk]['Type'] = trim($aaa[0]);
}
//print_r($collist);
//print_r($column);
for ($i=0;$i<$sumdata;$i++)
{
if (! in_array($key[$i], $column)) continue ;
if (empty($add)){
$add="(";
}else{
$add=$add.",";
}
if (empty($val)){
$val="(";
}else{
$val=$val.",";
}
if(in_array($collist[ $key[$i] ]['Type'] , $this->datanumtype) && trim($value[$i])=="") $value[$i] =0 ;
$add=$add. "`".$key[$i] ."`";
$val=$val. "'".$value[$i]."'";
}
foreach ($collist as $kk => $vv) {
if(in_array( $kk , $key)) continue ;
if ( $vv['Null'] == 'YES' ) continue ;
if (empty($add)){ $add="("; }else{ $add=$add.","; }
if (empty($val)){ $val="("; }else{ $val=$val.","; }
if(in_array($vv['Type'] , $this->datanumtype )) {
$add=$add . "`".$kk ."`";
$val=$val . "'0'";
}
else{
$add=$add . "`".$kk ."`";
$val=$val . "''";
}
}
$add=$add.")";
$val=$val.")";
$sql="INSERT INTO ".$table." ".$add." VALUES ".$val;
//echo $sql;
$this->sql = $sql;
if (mysqli_query($this->connect_db, $sql)){
return mysqli_insert_id($this->connect_db);
}else{
$this->_error($sql);
return false;
}
}
//แก้ไขข้อมูลแบบหลายฟิลล์
//$db->update_db("tabel",array("field"=>"value"),"where");
function update_db($table="table",$data="data",$where="where"){
$key = array_keys($data);
$value = array_values($data);
$sumdata = count($key);
$collist = $this->array_column($table) ;
$column = array_keys($collist );
foreach ($collist as $kkk => $vvv) {
$aaa = explode('(', $collist[$kkk]['Type']);
$collist[$kkk]['Type'] = trim($aaa[0]);
}
//print_r($collist);
//print_r($column);
$set="";
for ($i=0;$i<$sumdata;$i++)
{
if (! in_array($key[$i], $column)) continue ;
if (!empty($set)){
$set=$set.",";
}
if(in_array($collist[ $key[$i] ]['Type'], $this->datanumtype) && ( trim($value[$i])=="" || trim($value[$i])=="'") ) $value[$i] =0 ;
$set=$set . $key[$i] ."='".$value[$i]."'";
}
$sql="UPDATE ".$table." SET ".$set." WHERE ".$where;
//echo $sql;
$this->sql = $sql;
if (mysqli_query($this->connect_db, $sql)){
return true;
}else{
$this->_error($sql);
return false;
}
}
//แก้ไขข้อมูลแบบฟิลล์เดียว
//$db->update("table","set","where");
function update($table="table",$set="set",$where="where"){
$sql="UPDATE ".$table." SET ".$set." WHERE ".$where;
if (mysqli_query($this->connect_db, $sql)){
return true;
}else{
$this->_error();
return false;
}
}
//ลบข้อมูล
//$db->del("table","where");
function del($table="table",$where="where"){
$sql="DELETE FROM ".$table." WHERE ".$where;
if (mysqli_query($this->connect_db, $sql)){
return true;
}else{
$this->_error();
return false;
}
}
//นับจำนวนแถวข้อมูล
//$db->num_rows("table","field","where");
function num_rows($table="table",$field="field",$where="where") {
if (trim($where)=="") {
$where = "";
} else {
$where = " WHERE ".$where;
}
$sql = "SELECT ".$field." FROM ".$table.$where;
if($res = mysqli_query($this->connect_db, $sql)){
return mysqli_num_rows($res);
}else{
$this->_error();
$this->sql = $sql ;
return false;
}
}
//Query ข้อมูล
//$res = $db->select_query('SELECT field FROM table WHERE where');
//$row = $db->fetch($res);
function select_query($sql="sql"){
$this->sql = $sql;
if ($res = mysqli_query($this->connect_db, $sql)){
return $res;
}else{
$this->_error();
return false;
}
}
//นับจำนวนแถวข้อมูล
//$res = $db->select_query('SELECT field FROM table WHERE where');
//$rows = $db->rows($res);
function rows($sql="sql"){
// if(! $sql || $sql="sql") return false;
if ($res = mysqli_num_rows($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
//ดึงค่า array
//$res = $db->select_query('SELECT field FROM table WHERE where');
//while ($arr = $db->fetch($res)) {
// echo $arr['a']." - ".$arr['c']."<br>\n";
//}
function fetch($sql="sql"){
if ($res = mysqli_fetch_assoc($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
//แสดงข้อความผิดพลาด
function _error(){
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
//exit();
}
if(mysqli_errno($this->connect_db)){
$this->error[]=mysqli_errno($this->connect_db) . mysqli_error($this->connect_db);
echo "Error :" . mysqli_errno($this->connect_db) . mysqli_error($this->connect_db);
//exit ;
}
}
}
?>