| <?php/**
 * Mysqli class
 *
 * @author Ruins
 * @version v1.0 2009-08-18
 */Class Db_mysqli {
 protected $mysqli;
 protected $sql;
 protected $rs;
 protected $query _num = 0;
 protected $fetch _mode = MYSQLI_ASSOC;
 protected $cache _dir = './cache/';
 protected $cache _time = 1800;
 
 Public function __construct ($dbhost, $dbuser, $dbpass, $dbname) {
 $this->mysqli = new Mysqli ($dbhost, $dbuser, $dbpass, $dbname);
 if (Mysqli_connect_errno ()) {
 $this->mysqli = false;
 Echo ' Die ();
 } else {
 $this->mysqli->set_charset ("UTF8");
 }
 }
 
 Public Function __destruct () {
 $this->free ();
 $this->close ();
 }
 
 protected function free () {
 @ $this->rs->free ();
 }
 
 protected function Close () {
 $this->mysqli->close ();
 }
 
 protected function Fetch () {
 return $this->rs->fetch_array ($this->fetch_mode);
 }
 
 protected function Getquerysql ($sql, $limit = null) {
 if (@preg_match ("/[0-9]+" (, []?[ 0-9]+)?/is ", $limit) &&!preg_match ("/Limit [0-9]+ (, []?[ 0-9]+)? $/is ", $sql)) {
 $sql. = "LIMIT". $limit;
 }
 return $sql;
 }
 
 protected function Get_cache ($sql, $method) {
 Include_once './cache.php ';//If __autoload () is used in the frame, you can use the file without loading
 $cache = new Cache ($this->cache_dir, $this->cache_time);
 $cache _file = MD5 ($sql. $method);
 $res = $cache->get_cache ($cache _file);
 if (! $res) {
 $res = $this-> $method ($sql);
 $cache->set_cache ($cache _file, $res);
 }
 return $res;
 }
 
 Public Function Query_num () {
 return $this->query_num;
 }
 
 Public Function Set_cache_dir ($cache _dir) {
 $this->cache_dir = $cache _dir;
 }
 
 Public Function Set_cache_time ($cache _time) {
 $this->cache_time = $cache _time;
 }
 
 Public Function query ($sql, $limit = null) {
 $sql = $this->getquerysql ($sql, $limit);
 $this->sql = $sql;
 $this->rs = $this->mysqli->query ($sql);
 if (! $this->rs) {
 echo "Die ();
 } else {
 $this->query_num++;
 return $this->rs;
 }
 }
 
 Public Function GetOne ($sql) {
 $this->query ($sql, 1);
 $this->fetch_mode = Mysqli_num;
 $row = $this->fetch ();
 $this->free ();
 return $row [0];
 }
 
 Public Function Get_one ($sql) {
 return $this->getone ($sql);
 }
 
 Public Function Cache_one ($sql) {
 $sql = $this->getquerysql ($sql, 1);
 return $this->get_cache ($sql, ' getone ');
 }
 
 Public Function GetRow ($sql, $fetch _mode = Mysqli_assoc) {
 $this->query ($sql, 1);
 $this->fetch_mode = $fetch _mode;
 $row = $this->fetch ();
 $this->free ();
 return $row;
 }
 
 Public Function Get_row ($sql, $fetch _mode = Mysqli_assoc) {
 return $this->getrow ($sql);
 }
 
 Public Function Cache_row ($sql) {
 $sql = $this->getquerysql ($sql, 1);
 return $this->get_cache ($sql, ' getRow ');
 }
 
 Public Function GetAll ($sql, $limit = null, $fetch _mode = Mysqli_assoc) {
 $this->query ($sql, $limit);
 $all _rows = Array ();
 $this->fetch_mode = $fetch _mode;
 while ($rows = $this->fetch ()) {
 $all _rows[] = $rows;
 }
 $this->free ();
 return $all _rows;
 }
 Public Function Get_all ($sql, $limit = null, $fetch _mode = Mysqli_assoc) {
 return $this->getall ($sql);
 }
 
 Public Function Cache_all ($sql, $limit = null) {
 $sql = $this->getquerysql ($sql, $limit);
 return $this->get_cache ($sql, ' getAll ');
 }
 
 Public Function insert_id () {
 return $this->mysqli->insert_id ();
 }
 
 Public function Escape ($STR) {
 if (Is_array ($STR)) {
 foreach ($str as $key => $val) {
 $STR [$key] = $this->escape ($val);
 }
 } else {
 $str = Addslashes (Trim ($STR));
 }
 return $str;
 }
 }
 Usage
 $db = new Db_mysqli (' localhost ', ' root ', 111222, ' dict ');
 $db->set_cache_time (10);
 $db->set_cache_dir ('./cache/sql/');
 $sql = "SELECT * from Words ORDER by word_id limit 10,10";
 $res 1 = $db->get_all ($sql);
 $res 2 = $db->cache_all ($sql);
 
 echo $db->query_num (), ' <br> ';
 ?>
 |