MYSQLI operation class

Source: Internet
Author: User
& Amp; lt ;? Php *** Mysqli class *** @ author ruins * @ versionv1.02009-08-18 * @ linkanerg.cn * classdb_mysqli {protected $ mysqli; p...
 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 () {ret Urn $ 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 framework, you do not need to load the file $ cache = new cache ($ this-> cache_dir, $ this-> cache_time) here ); $ 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-> s Ql = $ 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"; $ res1 = $ db-> get_all ($ SQL ); $ res2 = $ db-> cache_all ($ SQL); echo $ db-> query_num (),'
';?>

Mysqli objects mainly control connections between PHP and MySQL database servers, select databases, send SQL statements to MySQL servers, and set character sets, these tasks are completed through the constructor, member method, and member attribute declared in this class. The member methods and member attributes declared in the mysqli class are listed in tables 13-1 and 13-2.

Table 13-1 member methods in mysqli class (33 in total)

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.