Guest User

Untitled

a guest
Jul 31st, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. <?
  2. define('__HOST__',"localhost");
  3. define('__USERNAME__',"root");
  4. define('__PASSWORD__',"");
  5. define('__DATABASE__',"sampledatabase");
  6.  
  7.  
  8. // the sql singelton object of _mysql class
  9. function sql($query=null,$return_result = false) {
  10.     global $_mysql;
  11.     if (!isset($_mysql)) {
  12.         $_mysql = new _mysql( __HOST__, __USERNAME__, __PASSWORD__ , __DATABASE__ );
  13.     }
  14.     if ($query!==null) {
  15.         return $_mysql->execute($query,$return_result);
  16.     }
  17.     return $_mysql;
  18. }
  19.  
  20. // sample 1 --> insert
  21. /*
  22. $row = array("id"=>"1", "name" => "John" , "age" => "30");
  23. $result_id = sql()->insert("persons",$row);
  24. if ( $result_id != null  ) {
  25.     echo "inserted new row!";
  26. } else {
  27.     echo mysql_error();
  28. }
  29. */
  30.  
  31. // sample 2 --> update
  32. /*
  33. $row = array("name" => "John" , "age" => "31");
  34. $affected_rows = sql()->update("persons",$row," id = 1 ");
  35. if ($affected_rows) {
  36.     echo "$affected_rows row(s) updated!";
  37. } else {
  38.     if ($err = mysql_error()) {
  39.         echo $err;
  40.     } else {
  41.         echo "No rows affected!";
  42.     }
  43. }
  44. */
  45.  
  46.  
  47.  
  48.  
  49. // THE MYSQL WRAPPER CLASS
  50. class _mysql {
  51.     private $link;
  52.     private $result;
  53.  
  54.     // BASIC
  55.     function __construct($host,$username=null,$password=null,$database=null) {
  56.         $this->connect($host,$username=null,$password=null,$database=null);
  57.         return $this;
  58.     }
  59.    
  60.     function __destruct() {
  61.         $this->close();
  62.     }
  63.    
  64.     function connect($host,$username=null,$password=null,$database=null) {
  65.         if (is_array($host)) extract($host);
  66.         $this->link = mysql_connect($host,$username,$password);
  67.         mysql_select_db($database);
  68.         return $this->result;
  69.     }
  70.    
  71.     function close() {
  72.         @mysql_close($this->link);
  73.     }
  74.    
  75.     function execute($q,$return_result=false) {    
  76.         $this->result = mysql_query($q);
  77.         return ($return_result) ? $this->result : $this;
  78.     }
  79.     // DATA ADJUSTMENTS
  80.     public function secure($data) {
  81.         if (is_array($data)) {
  82.             foreach ($data as $i=>$d) {
  83.                 $data[$i] = mysql_real_escape_string($d);
  84.             }
  85.         } else {
  86.             $data = mysql_real_escape_string($data);
  87.         }
  88.         return $data;
  89.     }
  90.  
  91.     // QUERY BUILDING
  92.     public function generate_fields($data) { // note: All values get secured
  93.         $fields = $values = array();
  94.         foreach ($data as $key=>$value) {
  95.                 $fields[]="`".$key."`";
  96.                 $values[]="\"".$this->secure($value)."\"";
  97.         }
  98.         return array( $fields, $values );
  99.     }
  100.  
  101.        
  102.     // COMMON QUERIES
  103.     public function insert($table,$data) { // inserts a single row into table
  104.         list($fields,$values) = $this->generate_fields($data);
  105.         $fields="(".implode(",",$fields).")";
  106.         $values="(".implode(",",$values).")";
  107.         $q = "insert into `$table` {$fields} values {$values};";
  108.         $this->execute($q);
  109.         return mysql_insert_id();      
  110.     }
  111.    
  112.     public function update($table,$data,$where,$limit='') {
  113.         if ($where) $where="where $where";
  114.         if ($limit) $limit="limit $limit";     
  115.        
  116.         list( $fields, $values ) = $this->generate_fields($data);
  117.         foreach ($fields as $key=>$field) {
  118.             $updates[]="{$field}={$values[$key]}";
  119.         }
  120.         $updates=implode(",",$updates);
  121.         $q="update `$table` set {$updates} $where $limit;";
  122.         $this->execute($q);
  123.         return mysql_affected_rows();
  124.     }
  125.    
  126.        
  127.     public function delete($table,$where='',$limit='') {
  128.         if ($where) $where="where $where";
  129.         if ($limit) $limit="limit $limit";
  130.         $this->execute("delete from `$table` $where $limit;");
  131.         return mysql_affected_rows();
  132.     }
  133.  
  134. }
  135. ?>
Add Comment
Please, Sign In to add comment