Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- var $db_host;
- var $db_name;
- var $db_user;
- var $db_password;
- function DB(){
- $this->db_host = "mysql4.000webhost.com";
- $this->db_name = "a9684274_web";
- $this->db_user = "a9684274_web";
- $this->db_password = "********";
- }
- function insert($table, $keys, $values){
- $mysqli = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name);
- $sql = "INSERT INTO ".$table." (";
- if(is_array($keys)){
- if(!is_array($values)){return false;}
- if(count($values) != count($keys)){return false;}
- $keys = implode(", ", $keys);
- $sql .= $keys;
- $sql .= ") VALUES (";
- $types = "";
- $counter = 0;
- foreach($values as $val){
- $values[$counter] = strip_tags($val);
- if($types != ""){$sql .= ", ";}
- $sql .= "?";
- if(gettype($val) == "integer"){$types .= "i";}elseif(gettype($val) == "double"){$types .= "d";}else{$types .= "s";}
- $counter++;
- }
- $sql .= ")";
- }else{
- $values = strip_tags($val);
- $sql .= $keys;
- $sql .= ") VALUES (";
- $sql .= "?";
- if(gettype($values) == "integer"){$types = "i";}elseif(gettype($values) == "double"){$types = "d";}else{$types = "s";}
- $sql .= ")";
- $values = array($values);
- }
- if($stmt = $mysqli->prepare($sql)){
- call_user_func_array('mysqli_stmt_bind_param', array_merge (array($stmt, $types), $values));
- if($stmt->execute()){
- $mysqli->close();
- return true;
- }
- }
- $mysqli->close();
- return false;
- }
- function select($table, $keys = "", $values = "", $order = "", $direction = "", $limit = "", $amount = ""){
- $mysqli = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name);
- $sql = "SELECT * FROM ".$table;
- if($keys != "" && $values != ""){
- if(is_array($keys)){
- if(!is_array($values)){return false;}
- if(count($values) != count($keys)){return false;}
- $sql .= " WHERE ";
- $counter = 0;
- foreach($values as $val){
- if($counter > 0){$sql .= " AND ";}
- $sql .= $keys[$counter]."='".$mysqli->real_escape_string($val)."'";
- $counter++;
- }
- }else{
- $sql .= " WHERE ".$keys."='".$mysqli->real_escape_string($values)."'";
- }
- }
- if($order != "" && $direction != ""){
- $sql .= " ORDER BY ".$order." ".$direction;
- if(is_int($limit)){
- $sql .= " LIMIT ".$limit;
- if(is_int($amount)){
- $sql .= ", ".$amount;
- }
- }
- }
- if($result = $mysqli->query($sql)){
- $mysqli->close();
- return $result;
- }
- $mysqli->close();
- return false;
- }
- function update($table, $keys, $values, $where_keys, $where_values){
- $mysqli = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name);
- $sql = "UPDATE ".$table." SET ";
- if(is_array($keys)){
- if(!is_array($values)){return false;}
- if(count($values) != count($keys)){return false;}
- for($x = 0; $x < count($keys); $x++){
- if($x > 0){$sql .= ", ";}
- $sql .= $keys[$x]."='".$mysqli->real_escape_string(strip_tags($values[$x]))."'";
- }
- }else{
- $sql .= $keys."='".$mysqli->real_escape_string(strip_tags($values))."'";
- }
- if(is_array($where_keys)){
- if(!is_array($where_values)){return false;}
- if(count($where_keys) != count($where_values)){return false;}
- $sql .= " WHERE ";
- for($x = 0; $x < count($where_keys); $x++){
- if($x > 0){$sql .= " AND ";}
- $sql .= $where_keys[$x]."='".$mysqli->real_escape_string($where_values[$x])."'";
- }
- }else{
- $sql .= " WHERE ".$where_keys."='".$mysqli->real_escape_string($where_values)."'";
- }
- if($result = $mysqli->query($sql)){
- $mysqli->close();
- return $result;
- }
- $mysqli->close();
- return false;
- }
- function delete($table, $keys, $values){
- $mysqli = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name);
- $sql = "DELETE FROM ".$table." WHERE ";
- if(is_array($keys)){
- if(!is_array($values)){return false;}
- if(count($values) != count($keys)){return false;}
- for($x = 0; $x < count($keys); $x++){
- if($x > 0){$sql .= " AND ";}
- $sql .= $keys[$x]."='".$mysqli->real_escape_string($values[$x])."'";
- }
- }else{
- $sql .= $keys."='".$mysqli->real_escape_string($values)."'";
- }
- if($result = $mysqli->query($sql)){
- $mysqli->close();
- return $result;
- }
- $mysqli->close();
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement