Phpcmsv9 database operation class mysqli supports php7-PHP source code

Source: Internet
Author: User
Phpcmsv9 database operation class mysqli supports php7
 Config = $ config; if ($ config ['autoconnect '] = 1) {$ this-> connect ();}} /*** truly enable the database connection ** @ return void */public function connect () {// var_dump ($ this-> config); if (! $ This-> link = @ new mysqli ($ this-> config ['hostname'], $ this-> config ['username'], $ this-> config ['password'], $ this-> config ['database'], $ this-> config ['dbport']) {$ this-> halt ('Can not connect to MySQL server'); return false;} if ($ this-> link-> connect_errno) {$ this-> halt ("Connect failed: % s \ n", $ this-> link-> connect_error); exit ();} switch ($ this-> config ['charset']) {case 'utf8': $ query_string = "SE T condition = utf8, CHARACTER_SET_CONNECTION = utf8, CHARACTER_SET_DATABASE = utf8, Condition = utf8, Condition = utf8, COLLATION_CONNECTION = condition, COLLATION_DATABASE = condition, COLLATION_SERVER = condition, SQL _mode = ''"; break; case 'gbk': $ query_string = "SET CHARACTER_SET_CLIENT = GBK, CHARACTER_SET_CONNECTION = gbk, CHARACTER_SET_DAT ABASE = gbk, bytes = gbk, CHARACTER_SET_SERVER = gbk, COLLATION_CONNECTION = gbk_chinese_ci, COLLATION_DATABASE = Beijing, COLLATION_SERVER = Beijing, SQL _mode = ''"; break; default: $ error = "Db Error: charset is Invalid"; $ this-> halt ($ error);} // declare the encoding if (! $ This-> link-> query ($ query_string) {$ this-> halt ("Db Error :". mysqli_error ($ this-> link);} $ this-> database = $ this-> config ['database']; return $ this-> link ;} /*** database query execution method * @ param $ SQL the SQL statement to be executed * @ return query resource handle */private function execute ($ SQL) {// echo"
". $ SQL; if (! Is_resource ($ this-> link) {$ this-> connect () ;}$ this-> lastqueryid = $ this-> link-> query ($ SQL ); // $ this-> halt (mysqli_error ($ this-> link), $ SQL); $ this-> querycount ++; return $ this-> lastqueryid ;} /*** execute SQL query * @ param $ data the field value to be queried [for example, 'name', 'gender ', 'Birthday'] * @ param $ table data table * @ param $ where query condition [example 'name' = '$ name'] * @ param $ limit return result range [example: 10 or 10, 10 is blank by default] * @ param $ order sorting method [default sort by database] * @ par Am $ group: [null by default] * @ param $ key returns the array key name sorting * @ return array query result set count group */public function select ($ data, $ table, $ where = '', $ limit ='', $ order = '', $ group ='', $ key = '') {$ where = ''? '': 'Where'. $ WHERE; $ order = ''? '': 'Order by'. $ ORDER; $ group = ''? '': 'Group by'. $ GROUP; $ limit = ''? '': 'Limit '. $ limit; $ field = explode (',', $ data); array_walk ($ field, array ($ this, 'Add _ special_char ')); $ data = implode (',', $ field); $ SQL = 'select '. $ data. 'from ''. $ this-> config ['database']. ''. ''. $ table. '''. $ where. $ group. $ order. $ limit; $ this-> execute ($ SQL); if (! $ This-> lastqueryid) {return $ this-> lastqueryid;} $ datalist = array (); while ($ rs = $ this-> fetch_next ())! = False) {if ($ key) {$ datalist [$ rs [$ key] = $ rs;} else {$ datalist [] = $ rs ;}} $ this-> free_result (); return $ datalist;}/*** obtain the field value to be queried for a single record query * @ param $ data [example: 'name ', 'gender ', 'Birthday'] * @ param $ table data table * @ param $ where query condition * @ param $ order sorting method [default sorting by database] * @ param $ group [blank by default] * @ return array/null data query result set, if not, null */public function get_one ($ data, $ table, $ where = '', $ order = is returned. '', $ Group ='') {$ where = ''? '': 'Where'. $ WHERE; $ order = ''? '': 'Order by'. $ ORDER; $ group = ''? '': 'Group '. $ group; $ limit = 'limit 1'; $ field = explode (',', $ data); array_walk ($ field, array ($ this, 'Add _ special_char '); $ data = implode (', ', $ field); $ SQL = 'select '. $ data. 'from ''. $ this-> config ['database']. ''. ''. $ table. '''. $ where. $ group. $ order. $ limit; $ this-> execute ($ SQL); $ res = $ this-> fetch_next (); $ this-> free_result (); return $ res ;} /*** traverse query result set * @ param $ type return result set type * MYSQLI _ ASSOC, MYSQL_NUM and MYSQL_BOTH * @ return array */public function fetch_next ($ type = MYSQLI_ASSOC) {$ res = mysqli_fetch_array ($ this-> lastqueryid, $ type); if (! $ Res) {$ this-> free_result ();} return $ res;}/*** release query resource * @ return void */public function free_result () {if (is_resource ($ this-> lastqueryid) {mysqli_free_result ($ this-> lastqueryid); $ this-> lastqueryid = null ;}} /*** directly execute SQL query * @ param $ SQL query SQL statement * @ return boolean/query resource if it is a query statement, return the resource handle, otherwise, true/false */public function query ($ SQL) {// echo"
". $ SQL; return $ this-> execute ($ SQL);}/*** execute the add record operation * @ param $ data to add data. the parameter is an array. The array key is the field value, and the array value is the data value * @ param $ table data table * @ return boolean */public function insert ($ data, $ table, $ return_insert_id = false, $ replace = false) {if (! Is_array ($ data) | $ table = ''| count ($ data) = 0) {return false;} $ fielddata = array_keys ($ data ); $ valuedata = array_values ($ data); array_walk ($ fielddata, array ($ this, 'Add _ special_char '); array_walk ($ valuedata, array ($ this, 'escape _ string'); $ field = implode (',', $ fielddata); $ value = implode (',', $ valuedata); $ cmd = $ replace? 'Replace into': 'Insert into'; $ SQL = $ cmd. '''. $ this-> config ['database']. ''. ''. $ table. ''('. $ field. ') VALUES ('. $ value. ')'; $ return = $ this-> execute ($ SQL); return $ return_insert_id? $ This-> insert_id (): $ return;}/*** get the primary key number of the last added record * @ return int */public function insert_id () {return mysqli_insert_id ($ this-> link);}/*** perform the update record operation * @ param $ data the data to be updated. the parameter can be an array or a string, recommended array. * When the group key is a field value and the array value is a data value * when the value is a string, [example: 'name' = 'phpcms ', 'hits '= 'hits' + 1]. * [Example: array ('name' => 'phpcms ', 'password' => '123')] * array ('name' => '+ = 1', 'base' =>'-= 1') can be used for arrays '); the program will automatically resolve it to 'name' = 'name' + 1, 'base' = 'base'-1 * @ param $ table data table * @ param $ where condition for data update * @ return boolean */public function update ($ data, $ table, $ where = '') {if ($ table ='' or $ where = '') {return false;} $ where = 'where '. $ where; $ field = ''; if (is_string ($ data) & $ data! = '') {$ Field = $ data;} elseif (is_array ($ data) & count ($ data)> 0) {$ fields = array (); foreach ($ data as $ k =>$ v) {switch (substr ($ v, 0, 2) {case '+ =': $ v = substr ($ v, 2); if (is_numeric ($ v) {$ fields [] = $ this-> add_special_char ($ k ). '= '. $ this-> add_special_char ($ k ). '+ '. $ this-> escape_string ($ v, '', false);} else {continue;} break; case '-=': $ v = substr ($ v, 2 ); if (is_numeric ($ v) {$ fields [] = $ This-> add_special_char ($ k ). '= '. $ this-> add_special_char ($ k ). '-'. $ this-> escape_string ($ v, '', false);} else {continue;} break; default: $ fields [] = $ this-> add_special_char ($ k ). '= '. $ this-> escape_string ($ v); }}$ field = implode (',', $ fields);} else {return false;} $ SQL = 'update ''. $ this-> config ['database']. ''. ''. $ table. ''set '. $ field. $ where; return $ this-> execute ($ SQL);}/*** delete record operation * @ Param $ table data table * @ param $ where: condition for deleting data. it cannot be blank. * To clear a table, use the empty method * @ return boolean */public function delete ($ table, $ where) {if ($ table = ''| $ where ='') {return false;} $ where = 'where '. $ where; $ SQL = 'delete FROM ''. $ this-> config ['database']. ''. ''. $ table. '''. $ where; return $ this-> execute ($ SQL);}/*** obtain the number of results affected by the last database operation * @ return int */public function affected_rows () {return mysql_affected_rows ($ this-> link);}/*** get the primary key of the data table * @ pa Ram $ table data table * @ return array */public function get_primary ($ table) {$ this-> execute ("show columns from $ table "); while ($ r = $ this-> fetch_next () {if ($ r ['key'] = 'pri ') break ;} return $ r ['field'];}/*** obtain the table Field * @ param $ table data table * @ return array */public function get_fields ($ table) {$ fields = array (); $ this-> execute ("show columns from $ table"); while ($ r = $ this-> fetch_next ()) {$ fields [$ r ['Field'] = $ r ['type'];} return $ fields ;} /*** check for nonexistent fields * @ param $ table name * @ return array */public function check_fields ($ table, $ array) {$ fields = $ this-> get_fields ($ table); $ nofields = array (); foreach ($ array as $ v) {if (! Array_key_exists ($ v, $ fields) {$ nofields [] = $ v ;}} return $ nofields ;} /*** check whether the table exists * @ param $ table name * @ return boolean */public function table_exists ($ table) {$ tables = $ this-> list_tables (); return in_array ($ table, $ tables )? 1: 0;} public function list_tables () {$ tables = array (); $ this-> execute ("show tables "); while ($ r = $ this-> fetch_next () {$ tables [] = $ r ['Tables _ in _'. $ this-> config ['database'];} return $ tables ;} /*** check whether the field exists * @ param $ table name * @ return boolean */public function field_exists ($ table, $ field) {$ fields = $ this-> get_fields ($ table); return array_key_exists ($ field, $ fields);} public function Num_rows ($ SQL) {$ this-> lastqueryid = $ this-> execute ($ SQL); return mysqli_num_rows ($ this-> lastqueryid);} public function num_fields ($ SQL) {$ this-> lastqueryid = $ this-> execute ($ SQL); return mysqli_num_fields ($ this-> lastqueryid);} public function result ($ SQL, $ row) {$ this-> lastqueryid = $ this-> execute ($ SQL); return @ mysql_result ($ this-> lastqueryid, $ row);} public function error () {return @ mysq Li_error ($ this-> link);} public function errno () {return intval (@ mysqli_errno ($ this-> link);} public function version () {if (! Is_resource ($ this-> link) {$ this-> connect ();} return mysqli_get_server_info ($ this-> link);} public function close () {if (is_resource ($ this-> link) {@ mysqli_close ($ this-> link) ;}} public function halt ($ message = '', $ SQL = '') {if ($ this-> config ['debug']) {$ this-> errormsg ="MySQL Query:$ SQL
MySQL Error:". $ This-> error ()."
MySQL Errno:". $ This-> errno ()."
Message:$ Message
Errno (). "& msg =". urlencode ($ this-> error (). "'target = '_ blank' style = 'color: red'> Need Help? "; $ Msg = $ this-> errormsg; echo'

'. $ Msg .'

'; Exit;} else {return false;}/*** enclose the fields in reverse quotation marks, to ensure Database Security * @ param $ value array value */public function add_special_char (& $ value) {if ('*' = $ value | false! = Strpos ($ value, '(') | false! = Strpos ($ value, '.') | false! = Strpos ($ value, ''') {// does not process contains * or uses the SQL method.} Else {$ value = '''. trim ($ value ). ''';} if (preg_match ("/\ B (select | insert | update | delete) \ B/I", $ value )) {$ value = preg_replace ("/\ B (select | insert | update | delete) \ B/I", '', $ value) ;}return $ value ;} /*** enclose the field values in quotation marks, to ensure Database Security * @ param $ value array value * @ param $ key array key * @ param $ quotation */public function escape_string (& $ value, $ key = '', $ quotation = 1) {if ($ quotation) {$ q = '\ '';} else {$ q =' ';} $ Value = $ q. $ value. $ q; return $ value; }}// here's a rough replacement using mysqli: // use mysqli to replace if (! Function_exists ('MySQL _ result') {function mysql_result ($ result, $ number, $ field = 0) {mysqli_data_seek ($ result, $ number ); $ row = mysqli_fetch_array ($ result); return $ row [$ field] ;}}?>

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.