Wakonda

myTinyTodo - MYSQL + PHP 7 (fichier class.db.mysql.php)

Jun 25th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     (C) Copyright 2009 Max Pozdeev <maxpozdeev@gmail.com>
  5.     Licensed under the GNU GPL v2 license. See file COPYRIGHT for details.
  6. */
  7.  
  8.  
  9. class DatabaseResult_Mysql
  10. {
  11.  
  12.     var $parent;
  13.     var $q;
  14.     var $query;
  15.     var $rows = NULL;
  16.     var $affected = NULL;
  17.     var $prefix = '';
  18.  
  19.     function __construct($query, &$h, $resultless = 0)
  20.     {
  21.         $this->parent = $h;
  22.         $this->query = $query;
  23.  
  24.         $this->q = mysqli_query($this->parent->dbh, $query);
  25.  
  26.         if(!$this->q)
  27.         {
  28.             throw new Exception($this->parent->error());
  29.         }
  30.     }
  31.  
  32.     function affected()
  33.     {
  34.         if(is_null($this->affected))
  35.         {
  36.             $this->affected = mysqli_affected_rows($this->parent->dbh);
  37.         }
  38.         return $this->affected;
  39.     }
  40.  
  41.     function fetch_row()
  42.     {
  43.         return mysqli_fetch_row($this->q);
  44.     }
  45.  
  46.     function fetch_assoc()
  47.     {
  48.         return mysqli_fetch_assoc($this->q);
  49.     }
  50.  
  51.     function rows()
  52.     {
  53.         if (!is_null($this -> rows)) return $this->rows;
  54.         $this->rows = mysqli_num_rows($this->q);
  55.         return $this->rows;
  56.     }
  57. }
  58.  
  59. class Database_Mysql
  60. {
  61.     var $dbh;
  62.     var $error_str;
  63.  
  64.     function __construct()
  65.     {
  66.     }
  67.  
  68.     function connect($host, $user, $pass, $db)
  69.     {
  70.         if(!$this->dbh = @mysqli_connect($host,$user,$pass))
  71.         {
  72.             throw new Exception(mysqli_connect_error());
  73.         }
  74.         if( @!mysqli_select_db($this->dbh, $db) )
  75.         {
  76.             throw new Exception($this->error());
  77.         }
  78.         return true;
  79.     }
  80.  
  81.     function last_insert_id()
  82.     {
  83.         return mysqli_insert_id($this->dbh);
  84.     }  
  85.    
  86.     function error()
  87.     {
  88.         return mysqli_error($this->dbh);
  89.     }
  90.  
  91.     function sq($query, $p = NULL)
  92.     {
  93.         $q = $this->_dq($query, $p);
  94.  
  95.         if($q->rows()) $res = $q->fetch_row();
  96.         else return NULL;
  97.  
  98.         if(sizeof($res) > 1) return $res;
  99.         else return $res[0];
  100.     }
  101.  
  102.     function sqa($query, $p = NULL)
  103.     {
  104.         $q = $this->_dq($query, $p);
  105.  
  106.         if($q->rows()) $res = $q->fetch_assoc();
  107.         else return NULL;
  108.  
  109.         if(sizeof($res) > 1) return $res;
  110.         else return $res[0];
  111.     }
  112.  
  113.     function dq($query, $p = NULL)
  114.     {
  115.         return $this->_dq($query, $p);
  116.     }
  117.  
  118.     function ex($query, $p = NULL)
  119.     {
  120.         return $this->_dq($query, $p, 1);
  121.     }
  122.  
  123.     private function _dq($query, $p = NULL, $resultless = 0)
  124.     {
  125.         if(!isset($p)) $p = array();
  126.         elseif(!is_array($p)) $p = array($p);
  127.  
  128.         $m = explode('?', $query);
  129.  
  130.         if(sizeof($p)>0)
  131.         {
  132.             if(sizeof($m)< sizeof($p)+1) {
  133.                 throw new Exception("params to set MORE than query params");
  134.             }
  135.             if(sizeof($m)> sizeof($p)+1) {
  136.                 throw new Exception("params to set LESS than query params");
  137.             }
  138.             $query = "";
  139.             for($i=0; $i<sizeof($m)-1; $i++) {
  140.                 $query .= $m[$i]. (is_null($p[$i]) ? 'NULL' : $this->quote($p[$i]));
  141.             }
  142.             $query .= $m[$i];
  143.         }
  144.         return new DatabaseResult_Mysql($query, $this, $resultless);
  145.     }
  146.  
  147.     function affected()
  148.     {
  149.         return  mysqli_affected_rows($this->dbh);
  150.     }
  151.  
  152.     function quote($s)
  153.     {
  154.         return '\''. addslashes($s). '\'';
  155.     }
  156.  
  157.     function quoteForLike($format, $s)
  158.     {
  159.         $s = str_replace(array('%','_'), array('\%','\_'), addslashes($s));
  160.         return '\''. sprintf($format, $s). '\'';
  161.     }
  162.  
  163.     function table_exists($table)
  164.     {
  165.         $table = addslashes($table);
  166.         $q = mysqli_query($this->dbh, "SELECT 1 FROM `$table` WHERE 1=0");
  167.         if($q === false) return false;
  168.         else return true;
  169.     }
  170. }
  171.  
  172. ?>
Add Comment
Please, Sign In to add comment