connect; } public function connect(){ if(!$this->connection){ $mycon = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); if($mycon){ $selectdb = @mysql_select_db($this->db_name,$mycon); if($selectdb){ $this->connection = true; return true; }else{ array_push($this->sqlErr, "Tidak Menemukan Database"); return false; } }else{ echo "Tidak bisa tersambung ke mysql"; return false; } }else{ return true; } } public function disconnect(){ if($this->connection){ if(@mysql_close()){ $this->connection = false; return true; }else{ return false; } } } public function select($table, $rows = '*', $where = null, $limit = null, $order = null){ $q = 'SELECT '.$rows.' FROM '.$table; if($where != null) $q .= ' WHERE '.$where; if($order != null) $q .= ' ORDER BY '.$order; if($limit != null) $q .= ' LIMIT '.$limit; //echo $q.'
'; //array_push($this->sqlErr, $q); if($this->tableExists($table)){ $query = @mysql_query($q); $query2 = mysql_query($q); $this->queryResult = $query2; if($query){ $this->numRows = mysql_num_rows($query); //retrieves the number of rows from the query for($i = 0; $i < $this->numRows; $i++) { $r = mysql_fetch_array($query); //mysql_fetch_array returns an array that corresponds to the fetched row $key = array_keys($r); //array_keys() returns the keys, numeric and string, from the input array. for($x = 0; $x < count($key); $x++) //count() counts all elements in an array { // Sanitizes keys so only alphavalues are allowed if(!is_int($key[$x])) { if(mysql_num_rows($query) > 1) $this->result[$i][$key[$x]] = $r[$key[$x]]; else if(mysql_num_rows($query) < 1) $this->result = null; else $this->result[$key[$x]] = $r[$key[$x]]; } } } return true; }else{ array_push($this->sqlErr, "Ada kesalahan pada query"); return false; } }else { echo "Table tidak ditemukan"; return false; } } public function insert($table,$values,$rows = null) { if($this->tableExists($table)){ $insert = 'INSERT INTO '.$table; if($rows != null)$insert .= ' ('.$rows.')'; for($i = 0; $i < count($values); $i++){ if(is_string($values[$i]))$values[$i] = '"'.$values[$i].'"'; } $values = implode(',',$values); $insert .= ' VALUES ('.$values.')'; //echo $insert; $insert_query = @mysql_query($insert); if($insert_query){ $this->idResult = mysql_insert_id(); return true; }else{ return false; } } } public function delete($table,$where = null){ if($this->tableExists($table)){ if($where == null){ $delete = 'DELETE '.$table; }else{ $delete = 'DELETE FROM '.$table.' WHERE '.$where; //equivalent to DELETE FROM table1 WHERE ...... } $delete = @mysql_query($delete); if($delete){ return true; }else{ return false; } }else{ return false; } } public function update($table,$rows,$where){ if($this->tableExists($table)){ for($i = 0; $i < count($where); $i++){ if($i%2 != 0){ if(is_string($where[$i])){ if(($i+1) != null)$where[$i] = '"'.$where[$i].'" AND '; else $where[$i] = '"'.$where[$i].'"'; } } } //$where = implode('',$where); $update = 'UPDATE '.$table.' SET '; $keys = array_keys($rows); for($i = 0; $i < count($rows); $i++){ if(is_string($rows[$keys[$i]])) $update .= $keys[$i].'="'.$rows[$keys[$i]].'"'; else $update .= $keys[$i].'='.$rows[$keys[$i]]; // add commas if($i != count($rows)-1) $update .= ','; } $update .= ' WHERE '.$where; //echo $update; $query = @mysql_query($update); if($query) { return true; } else { return false; } } else { return false; } } public function query($q){ $q2 = mysql_query($q); return $q2; } public function getResult(){ return $this->result; } public function getQueryResult(){ return $this->queryResult; } public function getId(){ return $this->idResult; } public function getSqlerr(){ return $this->sqlErr; } public function tableExists($table){ $tabl = explode(",", $table); $tableInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$tabl[0].'"'); if($tableInDb) { if(mysql_num_rows($tableInDb)==1) { return true; } else { return false; } } } } ?>