<? PHP /*************************************** *********************** * Descript: MySQL database operation management class * * Author: young adults * Email: wenadmin@sina.com * QQ: 303015292 * MSN: wenguangqing@hotmail.com **************************************** *********************/ /* * Example 1: Obtain the sequence * <? * $ Db = new dB (); * $ Result = $ db-> getseq ('art _ id', 2, 1 ); *?> */ /* * Example 2: Paging Query * <? * $ Db = new dB (); * $ Result = $ db-> & querypage ('select ID, name from table', 2, 10, db_fetch_assoc ); * Foreach ($ result as $ row) * Echo $ row ['id'], '------'. $ row ['name']. '<br> '; *?> */ /* * Example 3: insert data * <? * $ Db = new dB (); * $ Result = $ db-> execute ("insert into table (ID, name) values (1, 'name ')"); * If ($ result> 0) echo 'data inserted successfully '; * Else echo 'data insertion failed '; *?> */ /* * Define database connection options * @ Var db_host string Database Host name or address * @ Var db_name string Database Name * @ Var db_user string Database User Name * @ Var db_pwd string password of the Database User * @ Var db_pconnect Boolean whether to establish a persistent connection */ Define ('db _ host', 'localhost '); Define ('db _ name', 'test '); Define ('db _ user', 'root '); Define ('db _ pwd ',''); Define ('db _ pconnect ', true ); /* * Define the type of returned data query results * @ Var db_fetch_assoc int result call method: $ result ['name'] * @ Var db_fetch_num int result call method: $ result [0] * @ Var db_fetch_both INT: $ result ['name'] or $ result [0] * @ Var db_fetch_object int result call method: $ result-> name */ Define ('db _ fetch_assoc ', 0 ); Define ('db _ fetch_num ', 1 ); Define ('db _ fetch_both ', 2 ); Define ('db _ fetch_object ', 3 ); /* * Defines the name of the default sequence generator. */ Define ('db _ sequence_tablename', 'sequences '); Class dB { /* * Current database connection options */ VaR $ dbhost = db_host; VaR $ dbname = db_name; VaR $ dbtype = 'mysql '; VaR $ dbuser = db_user; VaR $ dbpwd = db_pwd; VaR $ PCNN = db_pconnect; /* * Current Database Connection */ VaR $ CNN = ''; /* * Data Query Result return type */ VaR $ queryfetchtype = db_fetch_assoc; /* * Initialization Function */ Function dB (){ $ This-> CNN = ($ this-> PCNN? Mysql_connect ($ this-> dbhost, $ this-> dbuser, $ this-> dbpwd ): Mysql_connect ($ this-> dbhost, $ this-> dbuser, $ this-> dbpwd) or $ This-> _ halt ('database connection error '); Mysql_select_db ($ this-> dbname, $ this-> CNN) or $ this-> _ halt ('database selection error '); } /* * Data Query Functions * * @ Param $ SQL string data query statement * @ Param $ fetchtype int return type of the Data Query Result * * @ Return array data query result */ Function & query ($ SQL, $ fetchtype = db_fetch_assoc ){ $ DATA = array (); $ Rs = & mysql_query ($ SQL, $ this-> CNN) or $ this-> _ halt ('data query error', $ SQL ); $ EXE = $ this-> _ getcommand ($ fetchtype ); While ($ ROW = & $ EXE ($ RS )) $ Data [] = & $ row; Return $ data; } /* * Paging data query function * * @ Param $ SQL string data query statement * @ Param $ page int current pre-query page number * @ Param $ pagesize: the number of records displayed on each page. * @ Param $ fetchtype int return type of the Data Query Result * * Data Query results and data paging information * @ Return array ('pagesize' => Number of entries displayed on each page * 'Recordcount' => total number of records * 'Pagecount' => total number of pages * 'Page' => current page number * 'Isfirst' => whether the first page is displayed * 'Isla' => whether to display the last page * 'Start' => the sequence number of the first record in the returned result * 'SQL' => the queried SQL statement * 'Data' => query results *) * Data Query results and data paging information */ Function & querypage ($ SQL, $ page = 1, $ pagesize = 20, $ fetchtype = db_fetch_assoc ){ $ Countsql = preg_replace ('| select. * From | I', 'select count (*) Count from', $ SQL ); $ Data ['pagesize'] = (INT) $ pagesize <1? 20: (INT) $ pagesize; $ Data ['recordcount'] = $ this-> getone ($ countsql, 'Count '); $ Data ['pagecount'] = Ceil ($ data ['recordcount']/$ data ['pagesize']); $ Data ['page'] = $ data ['pagecount'] = 0? 0: (INT) $ page <1? 1: (INT) $ page ); $ Data ['page'] = $ data ['page']> $ data ['pagecount']? $ Data ['pagecount']: $ data ['page']; $ Data ['isfirst'] = $ data ['page']> 1? False: true; $ Data ['isla'] = $ data ['page'] <$ data ['pagecount']? False: true; $ Data ['start'] = ($ data ['page'] = 0 )? 1: ($ data ['page']-1) * $ data ['pagesize'] + 1; $ Data ['SQL'] = $ SQL. 'limit'. ($ data ['start']-1). ','. $ data ['pagesize']; $ Data ['data'] = & $ this-> query ($ data ['SQL'], $ fetchtype ); Return $ data; } /* * Only 1st rows of data are returned for data query. * * @ Param $ SQL string data query statement * @ Param $ fetchtype int return type of the Data Query Result * * @ Return array data query result */ Function & queryrow ($ SQL, $ fetchtype = db_fetch_assoc ){ $ Rs = & mysql_query ($ SQL, $ this-> CNN) or $ this-> _ halt ('single row data query error', $ SQL ); $ EXE = $ this-> _ getcommand ($ fetchtype ); Return $ EXE ($ RS ); } /* * Only data in column N of row 1st is returned for data query. * * @ Param $ SQL string data query statement * @ Param $ field int returns the name or number of the data column. * * @ Return string returns the value of a single field. */ Function & getone ($ SQL, $ field = 0 ){ $ Rs = & mysql_query ($ SQL, $ this-> CNN) or $ this-> _ halt ('single Data Query error', $ SQL ); $ ROW = mysql_fetch_array ($ RS ); Return $ row [$ field]; } /* * Execute an SQL statement, including the execution statement of delect/insert/update... * * @ Param $ SQL string data query statement * * @ Return string returns the number of data rows affected by the statement. */ Function execute ($ SQL ){ $ Rs = mysql_query ($ SQL) or $ this-> _ halt ('Statement execution error', $ SQL ); Return mysql_affected_rows ($ this-> CNN ); } /* * Obtain the number of the last inserted data. */ Function getinsertid (){ Return mysql_insert_id ($ this-> CNN ); } /* * Sequence generator used to generate non-repeated sequence values * * @ Param $ fieldname string sequence name * @ Param $ step int serial number Interval * @ Param $ start int start value of the serial number * * @ Return int new Sequence Value */ Function getseq ($ fieldname, $ step = 1, $ start = 1 ){ $ Table = db_sequence_tablename; $ Step = (INT) $ step; $ Start = (INT) $ start; $ Rs = mysql_query ("Update $ table set seq_num = seq_num + ($ step) Where seq_name = '$ fieldname '"); If (! $ Rs | mysql_affected_rows ($ this-> CNN) <1 ){ $ Rs = mysql_query ('select * from '. db_sequence_tablename, $ this-> CNN ); If (! $ RS ){ $ SQL = "CREATE TABLE $ table ( Seq_name varchar (20) not null, Seq_num bigint (20) default 1 not null, Primary Key (seq_name ))"; $ Rs = mysql_query ($ SQL) or $ this-> _ halt ('sequence generator table creation failed', $ SQL ); } $ Rs = mysql_query ("insert into $ table values ('$ fieldname', $ start)") or $ This-> _ halt ('add new sequence error', $ SQL ); $ Seq = $ start; } Else { $ Seq = & $ this-> getone ("select seq_num from $ table where seq_name = '$ fieldname '"); } Return $ seq; } Function _ getcommand ($ fetchtype ){ Switch ($ fetchtype ){ Case db_fetch_assoc: $ EXE = 'mysql _ fetch_assoc '; break; Case db_fetch_num: $ EXE = 'mysql _ fetch_row '; break; Case db_fetch_both: $ EXE = 'mysql _ fetch_array '; break; Case db_fetch_object: $ EXE = 'mysql _ fetch_object '; break; Default: $ EXE = 'mysql _ fetch_array '; break; } Return $ EXE; } Function _ halt ($ MSG ){ $ Errno = mysql_errno ($ this-> CNN ); $ Errstr = mysql_error ($ this-> CNN ); Die ("Database Error: $ MSG <br> $ errno: $ errstr "); } } ?> |