<?php
ob_start();
@session_start();
$dbhost		= "localhost:3306";
$dbname		= "cp573292_easylight_db";
$dbusername 	= "cp573292_easy_sa";
$dbpassword 	= "pvOx*530";
$dbcharset	= "utf8";
$dbcollation 	= "utf8_general_ci";
//Site uel without slaze
$siteurl		="http://www.easy-light.com/";
//Class

class mysql_config {
var $dbconfig = array(
  'hostname' => "localhost:3306",
  'username' => "cp573292_easy_sa",
  'password' => "pvOx*530",
  'database' => "cp573292_easylight_db",
  'collation_connection' => "utf8_general_ci",
  'character_set' => "utf8"
);
	
function mysql_config(){
$GLOBALS['global_mysql_querydb_timer'] = 0;
$GLOBALS['global_mysql_querydb_counter'] = 0;
}
	
function get_querydb_timer() {
global $static_query_timer;
if(isset($static_query_timer)) return $static_query_timer;
else return $GLOBALS['global_mysql_querydb_timer'];
}
function get_querydb_counter() {
global $static_querydb_count;
if(isset($static_querydb_count)) return $static_querydb_count;
else return $GLOBALS['global_mysql_querydb_counter'];
}
function set_querydb_timer($value) {
global $static_query_timer;
if(isset($static_query_timer)) $static_query_timer = $value;
else $GLOBALS['global_mysql_querydb_timer'] = $value;
}
function set_querydb_counter($value) {
global $static_querydb_count;
if(isset($static_querydb_count)) $static_querydb_count = $value;
else $GLOBALS['global_mysql_querydb_counter'] = $value;
}
function set_hostname($value) {
$this->dbconfig['hostname'] = $value;
}
function set_username($value) {
$this->dbconfig['username'] = $value;
}
function set_password($value) {
$this->dbconfig['password'] = $value;
}
function set_database($value) {
$this->dbconfig['database'] = $value;
}
function set_collation_connection($value) {
$this->dbconfig['collation_connection'] = $value;
}
function set_character_set($value) {
$this->dbconfig['character_set'] = $value;
}
function get_configdb() {
return $this->dbconfig;
}
}

class mysql_operator extends mysql_config {
var $link = NULL;
var $result = NULL;
var $mysql_version = NULL;
var $string_error = array();
function set_configdb(&$array_dbconfig) {
$this->set_hostname($array_dbconfig['hostname']);
$this->set_username($array_dbconfig['username']);
$this->set_password($array_dbconfig['password']);
$this->set_database($array_dbconfig['database']);
$this->set_collation_connection(
$array_dbconfig['collation_connection']
);
$this->set_character_set($array_dbconfig['character_set']);
}
function set_error_msg($string){
array_push($this->string_error, $string);
}
function get_error_msg(){
while (count($this->string_error) - 1)
$string_output = '<b>MySQL Error : </b> '.
array_pop($this->string_error).'<br />';
return $string_output;
}
function mysql_operator(&$dbconfig) {
if(is_array($dbconfig)) $this->set_configdb($dbconfig);
else $this->set_configdb($dbconfig->get_configdb());
}
function set_mysql_version($value) {
$this->mysql_version = $value;
}
function get_mysql_version() {
return $this->mysql_version;
}
	
//function opendb($newlink = FALSE) {
//$this->link = mysqli_connect($this->dbconfig['hostname'],
//$this->dbconfig['username'],
//$this->dbconfig['password'],
//$newlink);
//if (!$this->link)
//$this->set_error_msg(
//mysqli_errno($this->link).':'.
//mysqli_error($this->link)
//);
//$this->set_mysql_version(mysqli_get_server_info());
//if (!mysqli_select_db ($this->dbconfig['database']))
//$this->set_error_msg(
//mysqli_errno($this->link).':'.
//mysqli_error($this->link)
//);
//if(!empty($this->dbconfig['character_set'])) {
//$this->querydb("SET CHARACTER SET ".
//$this->dbconfig['character_set'].";"
//);
//$this->querydb_counter_decrement();
//}
//if(!empty($this->dbconfig['collation_connection'])) {
//$this->querydb("SET collation_connection = ".
//$this->dbconfig['collation_connection'].";");
//$this->querydb_counter_decrement();
//}
//}
	
function opendb($newlink = FALSE) {
    $this->link = mysqli_connect(
        $this->dbconfig['hostname'],
        $this->dbconfig['username'],
        $this->dbconfig['password']
    );

    if (!$this->link) {
        $this->set_error_msg(
            mysqli_connect_errno() . ':' .
            mysqli_connect_error()
        );
        return;
    }

    $this->set_mysql_version(mysqli_get_server_info($this->link));

    if (!mysqli_select_db($this->link, $this->dbconfig['database'])) {
        $this->set_error_msg(
            mysqli_errno($this->link) . ':' .
            mysqli_error($this->link)
        );
    }

    if (!empty($this->dbconfig['character_set'])) {
        $this->querydb("SET CHARACTER SET " . $this->dbconfig['character_set'] . ";");
        $this->querydb_counter_decrement();
    }

    if (!empty($this->dbconfig['collation_connection'])) {
        $this->querydb("SET collation_connection = " . $this->dbconfig['collation_connection'] . ";");
        $this->querydb_counter_decrement();
    }
}	
	
function closedb() {
if (!mysqli_close($this->link))
$this->set_error_msg(mysqli_errno($this->link).':'.
mysqli_error($this->link)
);
}
	
function querydb($string_query) {
    $start_time = array_sum(explode(' ', microtime()));

    // ✅ ใช้ connection $this->link
    $this->result = mysqli_query($this->link, $string_query);

    $end_time = array_sum(explode(' ', microtime()));

    if (!$this->result) {
        $this->set_error_msg(
            mysqli_errno($this->link) . ':' . mysqli_error($this->link)
        );
    }

    $this->set_querydb_timer(
        $this->get_querydb_timer() + ($end_time - $start_time)
    );

    $this->querydb_counter_increment();
}
	
	
//function querydb($string_query) {
//$start_time = array_sum(explode(chr(32), microtime()));
//$this->result = mysqli_query ($string_query);
//$end_time = array_sum(explode(chr(32), microtime()));
//if (!$this->result)
//$this->set_error_msg(
//mysqli_errno($this->link).':'.mysqli_error($this->link)
//);
//$this->set_querydb_timer(
//$this->get_querydb_timer() + ($end_time - $start_time)
//);
//$this->querydb_counter_increment();
//}
//function list_fields($table) {
//$fields = mysql_list_fields(
//$this->dbconfig['database'], $table, $this->link
//);
//$columns = mysqli_num_fields($fields);
//for ($i = 0; $i < $columns; $i++)
//$array_fields_in_table[$i] = mysql_field_name($fields, $i);
//return $array_fields_in_table;
//}
	
function list_fields($table) {
    $array_fields_in_table = [];
    $query = "SHOW COLUMNS FROM `$table`";
    $result = mysqli_query($this->link, $query);

    if ($result) {
        while ($row = mysqli_fetch_assoc($result)) {
            $array_fields_in_table[] = $row['Field'];  // 'Field' = column name
        }
    }

    return $array_fields_in_table;
}
	
function querydb_counter_increment() {
$this->set_querydb_counter($this->get_querydb_counter()+1);
}
function querydb_counter_decrement() {
$this->set_querydb_counter($this->get_querydb_counter()-1);
}
function querydb_transac_begin() {
if($this->get_mysql_version() > 3)
$this->querydb("START TRANSACTION;");
}
function querydb_transac_commit() {
if($this->get_mysql_version() > 3) $this->querydb("COMMIT;");
}
function querydb_transac_rollback() {
if($this->get_mysql_version() > 3) $this->querydb("ROLLBACK;");
}
function querydb_transac_set_autocommit_to_zero() {
if($this->get_mysql_version() > 3)
$this->querydb("SET AUTOCOMMIT=0;");
}
function querydb_transac_check_for_rollback_or_commit() {
if(!$this->get_resultdb()) $this->querydb_transac_rollback();
else $this->querydb_transac_commit();
}
//function num_rows_in_table($table, $condition = NULL) {
//$num_rows = new mysql_operator($this->get_configdb());
//$num_rows->opendb();
//$num_rows->querydb("SELECT COUNT(*) FROM $table $condition;");
//$num_rows->closedb();
//return mysql_result($num_rows->get_resultdb(), 0);
//}
	
function num_rows_in_table($table, $condition = NULL) {
    $num_rows = new mysql_operator($this->get_configdb());
    $num_rows->opendb();

    $query = "SELECT COUNT(*) FROM `$table`";
    if (!empty($condition)) {
        $query .= " " . $condition;
    }
    $num_rows->querydb($query);

    $result = $num_rows->get_resultdb();
    $row = mysqli_fetch_row($result);  // ดึงแถวแรก (ผลลัพธ์ COUNT)

    $num_rows->closedb();
    return isset($row[0]) ? $row[0] : 0;  // คืนค่าจำนวนแถว
}
	
function has_data_in_table($table, $field, $value) {
$value = trim($value);
if(empty($field) or empty($value)) $sql_condtion = NULL;
else $sql_condtion = "WHERE ".$field."='".$value."'";
if($this->num_rows_in_table($table, $sql_condtion) > 0)return TRUE;
else return FALSE;
}
function reset_querydb() {
$this->set_resultdb(NULL);
}
function set_resultdb($resource) {
$this->result = $resource;
}
function get_resultdb() {
return $this->result;
}
//function get_resultdb_fetch_data() {
//return mysql_fetch_array($this->get_resultdb());
//}
//function get_resultdb_num_rows() {
//return mysql_num_rows($this->get_resultdb());
//}
//function get_resultdb_num_fields() {
//return mysql_num_fields($this->get_resultdb());
//}
//function get_last_insert_id(){
//return mysql_insert_id();
//}
//function get_resultdb_atrow($row = 0) {
//return mysql_result($this->get_resultdb(), $row);
//}
	
// ดึงข้อมูลแถวถัดไปในรูปแบบ associative array
function get_resultdb_fetch_data() {
    return mysqli_fetch_array($this->get_resultdb(), MYSQLI_ASSOC);
}

// จำนวนแถวใน result
function get_resultdb_num_rows() {
    return mysqli_num_rows($this->get_resultdb());
}

// จำนวนฟิลด์ใน result
function get_resultdb_num_fields() {
    return mysqli_num_fields($this->get_resultdb());
}

// คืนค่า ID ล่าสุดหลัง insert
function get_last_insert_id() {
    return mysqli_insert_id($this->link);
}

// คืนค่าที่อยู่ในแถว $row ของ result (เทียบกับ mysql_result เดิม)
function get_resultdb_atrow($row = 0) {
    $result = $this->get_resultdb();
    mysqli_data_seek($result, $row);
    $data = mysqli_fetch_row($result);
    return isset($data[0]) ? $data[0] : null;
}
}

$obj_dbconfig = new mysql_config();
$obj_dbconfig->set_hostname($dbhost);
$obj_dbconfig->set_username($dbusername);
$obj_dbconfig->set_password($dbpassword);

$obj_dbconfig->set_database($dbname);
$obj_dbconfig->set_character_set($dbcharset);
$obj_dbconfig->set_collation_connection($dbcollation);


//security
$sitekey = "obj-ann-2011-phpf"; 

?>