<?php
	
$hostname_connection = "localhost";
$database_connection =  "admin_image2015_db";
$username_connection = "admin";
$password_connection = "8645854a";
$connection = mysql_pconnect($hostname_connection, $username_connection, $password_connection) or trigger_error(mysql_error(),E_USER_ERROR); 
//mysql_query( "SET NAMES UTF8" );
mysql_set_charset('utf8',$connection);
mysql_select_db($database_connection, $connection);

	class db{
		var $host = 'localhost';
		var $user = 'admin';
		var $pass = '8645854a';
		var $db_name = 'admin_image2015_db';
		var $charset ='utf-8';
		var $sql;
                //var $url;
		//คำสั่งเพื่อเชื่อมต่อฐานข้อมูลโดยเลือกเทเบิ้ล
		function connect(){
				$con = mysql_connect($this->host,$this->user,$this->pass);
			if(!empty($con)){
			
			   mysql_select_db($this->db_name);
			   mysql_query('SET NAMES '.$this->charset);
			}else{
				echo mysql_error();
			}
			return $con;
		}
		//คำสั่งเพื่อดำเนืนการจัดการกับฐานข้อมูล
		function execute(){
			$con = $this->connect();
			if(!empty($con)){
				return mysql_query($this->sql);
			}
			return null;
		}
		
		function insert($table,$data){
			$field = "";
			$val = "";
			$i = 0;
			foreach($data as $k => $v){
				$field.=$k;
				$val .="'$v'";
				
				if($i<count($data)-1){
					$field.=',';
					$val.=',';
				}
				$i++;
			}
			$this->sql = "INSERT INTO $table($field) VALUES($val)";
			return mysql_query($this->sql);
		}

		/*function insert($table,$data) {
		$fields="";
		$values="";
		$i=1;
		foreach($data as $key=>$val){
		if($i!=1) { $fields.=", "; $values.=", "; }
		$fields.="$key";
		$values.="'$val'";
		$i++;
		}
		$sql = "INSERT INTO $table ($fields) VALUES ($values)";
		if(mysql_query($sql)) { return true; }
		else { die("SQL Error: ".$sql."".mysql_error()); return false;}
		}*/
		
		function delete($table,$field,$value){
			$this->sql = "DELETE FROM $table WHERE $field = $value";
			return mysql_query($this->sql);
		}
		
		function update($table, $data, $field, $value){
			$rows ="";
			$i=0;
			
			foreach($data as $k => $v){
				if($k!=$field){
					$rows.="$k ='$v'";
					if($i<count($data)-1){
						$rows.=',';
					}
					$i++;
				}
			}
			$this->sql = "UPDATE $table SET $rows WHERE $field = $value";
			return mysql_query($this->sql);
		}
	
		
		function executeRow(){
			$con = $this->connect();
			if(!empty($con)){
				$rs = mysql_query($this->sql);
				if(!empty($rs)){
					$row = mysql_fetch_array($rs);
					return $row;
				}
			}
			return null;
		}
		
		function conditions($table,$condition){
			$this->sql = "SELECT * FROM $table WHERE $condition";
			return $this;
		}
		
		
		function specifytable($tablemain,$table,$condition){
			$this->sql = "SELECT $tablemain FROM $table WHERE $condition";
			return $this;
		}
		
		function in($table,$field,$value){
			$_value ="";
			$count = 0;
			
			foreach($value as $v){
			$_value.="$v";
			
			//add comma
			if(count($value)!=$count +1){
				$_value.=",";
			}
			$count++;
		}
			$this->sql ="SELECT * FROM $table WHERE $field IN ($_value)";
			return $this;
	}

		function countdb($count,$total,$table,$column,$value,$group,$having){
			$this->sql = "SELECT COUNT($count) AS $total FROM $table WHERE $column = $value GROUP BY $group HAVING $group = $having";
			return $this;
		}
		
		function findAll($table){
			$con = $this->connect();
			if(!empty($con)){
				$this->sql = 'SELECT * FROM '.$table;
				return $this;
			}
			return null;
		}
		function findByPK($table,$column,$value){
			$this->sql = "SELECT * FROM $table WHERE $column = $value";
			return $this;
		}

		function findByPK2V($table,$column,$column2,$value,$value2){
			$this->sql = "SELECT * FROM $table WHERE $column = $value AND $column2 = $value2";
			return $this;
		}

		function findByPKin($table,$column,$value,$value2){
			$this->sql = "SELECT * FROM $table WHERE $column in ($value,$value2)";
			return $this;
		}

		function findGvPro($table,$column,$column2,$column3,$value,$value2,$value3){
			$this->sql = "SELECT * FROM $table WHERE $column = $value AND $column2 = $value2 AND $column3 != $value3 LIMIT 1";
			return $this;
		}

		function findSlip($bb,$bb_value,$bn,$bn_value,$bd,$bd_value,$bt,$bt_value){
			$this->sql = "SELECT * FROM tbbook WHERE $bb LIKE $bb_value AND $bn = $bn_value AND $bd LIKE $bd_value AND $bt LIKE $bt_value LIMIT 1";
			return $this;
		}

		function findSlipBook($bookid,$bb,$bb_value,$bn,$bn_value,$bd,$bd_value,$bt,$bt_value){
			$this->sql = "SELECT * FROM tbbook WHERE book_id != '".$bookid."' AND $bb LIKE '".$bb_value."' AND $bn LIKE '".$bn_value."' AND $bd LIKE '".$bd_value."' AND $bt LIKE '".$bt_value."' LIMIT 1";
			return $this;
		}

		function findByPK2($table,$column,$value){
			$this->sql = "SELECT * FROM $table WHERE $column LIKE '$value'";
			return $this;
		}

		function findByPK22($table,$column,$value){
			$this->sql = "SELECT * FROM $table WHERE $column LIKE '$value'";
			return $this;
		}

		function findByPK23($table,$column,$value,$column2,$value2,$column3,$value3){
			$this->sql = "SELECT * FROM $table WHERE $column LIKE '$value' AND $column2 LIKE '$value2' AND $column3 LIKE '$value3'";
			return $this;
		}
		
		function findorder($table,$value,$order){
			$this->sql = "SELECT * FROM $table WHERE $value order by $order asc ";
			return $this;
		}

		function findorderin2($table,$value,$order,$item,$item2,$order2){
			$this->sql = "SELECT * FROM $table WHERE $value order by case when $order in ($item,$item2) then -1 else $order2 end,$order2 asc";
			return $this;
		}
		
		function findorderin3($table,$value,$order,$item,$item2,$item3,$order2){
			$this->sql = "SELECT * FROM $table WHERE $value order by case when $order in ($item,$item2,$item3) then -1 else $order2 end,$order2 asc";
			return $this;
		}
		
		function findorderid($table,$column,$value,$order){
			$this->sql = "SELECT * FROM $table WHERE $column = $value order by $order asc";
			return $this;
		}

		function findorderid2($table,$column,$value,$value2,$order){
			$this->sql = "SELECT * FROM $table WHERE $column in ($value,$value2) order by $order asc";
			return $this;
		}
		
		function findorderdesc($table,$column,$value,$order){
			$this->sql = "SELECT * FROM $table WHERE $column = $value order by $order desc";
			return $this;
		}

		function findorderdescin($table,$column,$value,$value2,$order){
			$this->sql = "SELECT * FROM $table WHERE $column in ($value,$value2) order by $order desc";
			return $this;
		}

		function findorderdesclimit($table,$column,$value,$order){
			$this->sql = "SELECT * FROM $table WHERE $column = $value order by $order desc limit 0,10";
			return $this;
		}

		function findorderdesclimitin($table,$column,$value,$value2,$order){
			$this->sql = "SELECT * FROM $table WHERE $column in ($value,$value2) order by $order desc limit 0,10";
			return $this;
		}

		function findByPKno($table,$column,$value){
			$this->sql = "SELECT * FROM $table WHERE $column != $value";
			return $this;
		}
                function findByTable($table, $value){
                    $this->sql = "SELECT * FROM $table Where $value";
                    return $this;
                }
                function findByTable2($table1, $table2, $value){
                    $this->sql = "SELECT * FROM $table1, $table2 Where $value";
                    return $this;
                }
                function findByTable21($table1, $table2, $value1, $value2){
                    $this->sql = "SELECT * FROM $table1, $table2 Where $value1 && $value2";
                    return $this;
                }
                function findByTable31($table1, $table2, $table3, $value1, $value2){
                    $this->sql = "SELECT * FROM $table1, $table2, $table3 Where $value1 && $value2";
                    return $this;
                }
                function findByTable3($table1, $table2, $table3, $value1, $value2, $value3){
                    $this->sql = "SELECT * FROM $table1, $table2, $table3 Where $value1 && $value2 && $value3";
                    return $this;
                }
                function findByTable4($table1, $table2, $table3, $table4, $value1, $value2, $value3){
                    $this->sql = "SELECT * FROM $table1, $table2, $table3, $table4 Where $value1 && $value2 && $value3";
                    return $this;
                }
                function findByTable6($table1, $table2, $table3, $table4, $table5, $table6, $value1, $value2, $value3, $value4, $value5, $value6){
                    $this->sql = "SELECT * FROM $table1, $table2, $table3, $table4, $table5, $table6 Where $value1 && $value2 && $value3 && $value4 && $value5 && $value6";
                    return $this;
                }
                        function findByPK4($tablemain,$table1,$table2,$table3,$table4,$column1,$value1,$column2,$value2,$column3,$value3,$column4,$value4){
			$this->sql = "SELECT * FROM $tablemain,$table1,$table2,$table3,$table4 WHERE $column1 = $value1 && $column2 = $value2 && $column3 = $value3 && $column4 = $value4";
			return $this;
		}
		
		
		function findByAttributes($table,$attributes){
				$this->sql = "SELECT * FROM $table WHERE";
				$count = 0;
				
				foreach($attributes as $k => $v){
					if($count == 0){
						$this->sql.= " $k '$v'";
					}else{
						$this->sql.= " AND $k '$v'";
					}
					$count++;
				}
				return $this;
			}
                        function compare($table, $field, $value){
                            $this->sql = "select $ From $table where $field $value";
                            return $this;
                        }
	}


?>