PHP Write mysqli class _php tutorial with cached data function

Source: Internet
Author: User
Tags php write
Copy CodeThe code is as follows:
/**
* Mysqli class
*/
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 '

'. Mysqli_connect_error (). '

';
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 you use __autoload () in the frame, you can not load the file here
$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 "

". $this->mysqli->error."

";
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 (), '
';
?>

http://www.bkjia.com/PHPjc/325834.html www.bkjia.com true http://www.bkjia.com/PHPjc/325834.html techarticle Copy the code as follows: PHP/** * MYSQLI class * */Class Db_mysqli {protected $mysqli; protected $sql; protected $rs; protected $query _ num = 0; protected $fetch _mode = Mysqli_asso ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.