Start your MVC (2) Abstract database interface with PHP

zhaozj2021-02-12  138

Second, abstract database interface (using data operation management class)

When developing with MVC mode, the Model layer is responsible for all operations of the database. In order to manage the operation of the database, we need to define a database operation management class, which is replaced by all database operations, which is only the entire system. This database operation management class can operate directly, if other classes need to operate the database, it must be called and through this class.

The following DB class is a class.

getseq ('art_id', 2, 1) ; *?> * // * * EXAMPLE 2: Page query * & querypage ('select ID, name from table ", 2, 10, db_fetch_assoc * Foreach ($ RESULT AS $ ROW) * Echo $ ROW ['ID'], '------'. $ ROW ['Name']. '
'; *?> * ///// * * EXAMPLE 3: Insert data * execute ("INSERT INTO TABLE (ID, NAME) VALUES (1, 'Name')"); * IF ($ Result> 0) Echo 'Insert Data Success'; * Else Echo 'Insert Data Failed'; *?> * // * * Defines Database Piece 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 Database User's Password * @var DB_PCONNECT BOOLLAN 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); / * * Defines the type of the return data query result * @var db_fetch_assoc int result call mode: $ results ['name'] * @var db_fetch_num int @var db_fetch_num int Result Call: $ Result [0] * @var db_fetch_both int result Calling method: $ result ['name'] or $ result [0] * @var db_fetch_Object int result call mode: $ results->

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); Class DB {/ * * Current database joint option * / 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 $ cn = '; / * * Data query results 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 function * * @Param $ SQL STRING Data query statement * @Param $ fetchtype INT data query results return type * * @Return Array data query result * / function & query ($ sql, $ fetchtype = db_fetch_assoc) {$ data = array (); $ = & mysql_query ($ SQL, $ THIS-> CNN) OR $ this -> _ halt ('Data Quotes Errors', $ SQL); $ EXE = $ THIS -> _ getcommand ($ fetchtype); while ($ row = & $ est)) $ data [] = & $ Row; return $ data;} / * * Page Data Query Function * * @Param $ SQL String Data Query Statement * @Param $ Page Int Current Squi 201 * @Param $ PAGESIZE INT Multi-record * @

Param $ FETCHTYPE INT data query results return type * * Data query result, and data page information * @return array ('Pagesize' =>) The number of rods displayed per page * 'RecordCount' => total record number * 'PageCount' => Total page * 'Page' => Current page * 'isfirst' => Whether the first page * 'islast' => Whether the last page * 'start' => Return the first record of the result * ' SQL '=> Query SQL statement *' data '=> queries result *) * Data query results, as well as data page 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 [Page ']; $ Data [' isfirst '] = $ data [' Page ']> 1? False: True; $ data [' islast '] = $ data [' Page ']? 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;} / * * Data query only returns the data of the first line * * @Param $ SQL STRING Data query statement * @Param $ fetchtype Int Batterback Type * * @Return Array Data Query Results * / Function & QueryRow ($ SQL, $ FETCHPE = DB_FETCH_ASSOC) {$ r = & mysql_query ($ SQL, $ THIS-> CNN) OR $ this -> _ halt (' Single line data query error ', $ sql); $ exe = $ this -> _ getcommand ($ fetchtype); return $ estresses;} / * * Data query only returns the Northern Number N column * * * * @Param $ SQL STRING Data Query Statement * @Param $ Field INT Return Data Columns Name or Digital Serial No. * * @Return String Returns the value of a single field * / Function & Getone ($ SQL, $ FIELD = 0) {$ = & mysql_Query ($ SQL, $ THIS-> CNN) OR $ this -> _ halt ('single data query error', $ SQL); $ row = mysql_fetch_array ($ r); return $ row [$ FIELD];} / * * SQL statement, including delect / insert / update ..... @ @Param $ SQL STRING Data Query Statement * * @Return String Returns the number of data affected by the statement * / Function Execute ($ sql) {$ r = mysql_query ($ SQL) or $ this -> _ halt ('statement execution error ", $ SQL); RETURN MySQL_AFFECTED_ROWS ($ this-> CNN);} / * * Get the last plug-in data number * / function getInsertID () {RETURN mysql_insert_id ($ this-> cn);} / * * sequence generator, used To generate non-repetitive sequence values ​​* * @Param $ fieldName String sequence name * @Param $ STEP INT serial number * @Param $ start int serial number start value * * @

Return Int new sequence value * / function getseq ($ fieldname, $ step = 1, $ start = 1) {$ TABLE = db_sequence_tablename; $ step = (int) $ step; $ start = (int) $ start; $ r = mysql_query ("Update $ TABLE SEQ_NUM = SEQ_NUM ($ Step) Where seq_name = '$ inf - (! $ = @ @ | mSQL_AFFECTED_ROWS ($ this-> CNN) <1) {$ r = mysql_query (' Select * from '.db_sequence_tablename, $ this-> cnn); if (! $ R) {$ SQL = "Create Table $ TABLE (seq_name varchar (20) Not null, seq_num bigint (20) Default 1 Not Null, PRIMARY Key (seq_name) "; $ = mysql_query ($ SQL) or $ this -> _ Halt ('Create a sequence generator table failed', $ sql);} $ r = 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;

转载请注明原文地址:https://www.9cbs.com/read-6129.html

New Post(0)