- /**
- * Class DB
- * Database operations
- */
- Class DB {
- /**
- * @ Var
- * @ Return CDB
- */
- Private static $ db;
- /** Obtain the CDb class
- * @ Param $ table_name table name
- * @ Param string $ db_setting call the database configuration item
- * @ Param array $ db_config database configuration
- * @ Return CDb
- */
- Public static function cdb ($ table_name = '', $ db_setting = 'default', $ db_config = array ()){
- If (! Isset (self: $ db )){
- $ Db = new CDb ($ table_name, $ db_setting, $ db_config );
- Self: $ db = $ db;
- } Else {
- $ Db = self: $ db;
- }
- Return $ db;
- }
- /** Configuration
- * @ Param $ table_name table name
- * @ Param string $ db_setting call the database configuration item
- * @ Param array $ db_config database configuration
- * @ Return CDb
- */
- Public static function init ($ table_name = '', $ db_setting = 'default', $ db_config = array ()){
- Return self: cdb ($ table_name, $ db_setting, $ db_config );
- }
- /**
- * Delete a record
- * @ Param $ table name
- * @ Param $ condition: Delete a data condition. it cannot be blank. Can be an array
- * @ Return boolean
- */
- Public static function delete ($ table, $ condition ){
- $ Db = self: cdb ();
- $ Db-> setTableName ($ table );
- Return $ db-> delete ($ condition );
- }
- /**
- * Add a record
- * @ Param $ table name
- * @ Param array $ data the data to be added. The parameter is an array. The array key is the field value, and the array value is the data value.
- * @ Param bool $ return_insert_id whether to return the new ID number
- * @ Param bool $ replace whether to use the replace into method to add data
- * @ Return boolean
- */
- Public static function insert ($ table, $ data, $ return_insert_id = false, $ replace = false ){
- $ Db = self: cdb ();
- $ Db-> setTableName ($ table );
- Return $ db-> insert ($ data, $ return_insert_id, $ replace );
- }
- /**
- * Obtain the primary key number of the last added record
- * @ Return int
- */
- Public static function insertID (){
- $ Db = self: cdb ();
- Return $ db-> insert_id ();
- }
- /**
- * Perform the update record operation.
- * @ Param $ table name
- * @ Param $ data the data content to be updated. the parameter is an array.
- * If it is an array, the group key is the field value, and the array value is the data value.
- * [Example: array ('name' => 'lanmps ', 'password' => '123')]
- * Array ('name' => '+ = 1', 'base' =>'-= 1 '); the program will automatically resolve it to 'name' = 'name' + 1, 'base' = 'base'-1
- * String, in the following format:
- * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
- * @ Param $ where: conditions for updating data,
- * String, in the following format:
- * String [Example 1: "id = 1 and time> $ time"]
- * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
- * [Example: array ('name' => 'lanmps ', 'password' => '123')]
- * @ Return boolean
- */Bbs.it-home.org
- Public static function update ($ table, $ data, $ where ){
- $ Db = self: cdb ();
- $ Db-> setTableName ($ table );
- Return $ db-> update ($ data, $ where );
- }
- /**
- * Query a single record
- * @ Param array $ SQL query condition statement
- * @ Return array/null: query result set. If no result exists, null is returned.
- */
- Public static function fetchFirst ($ SQL ){
- $ Db = self: cdb ();
- Return $ db-> fetch ($ SQL );
- }
- /**
- * Execute an SQL query
- * @ Param $ SQL query condition
- * @ Return array number Group of query result sets
- */
- Public static function fetchAll ($ SQL ){
- $ Db = self: cdb ();
- Return $ db-> fetchAll ($ SQL );
- }
- /**
- * Directly execute SQL queries
- * @ Param $ SQL query SQL statements
- * @ Return
- */
- Public static function query ($ SQL ){
- $ Db = self: cdb ();
- Return $ db-> exec ($ SQL );
- }
- /**
- * Execute an SQL query
- * @ Param $ table name
- * @ Param $ where query condition
- * String, in the following format:
- * String [Example 1: "id = 1 and time> $ time"]
- * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
- * [Example: array ('name' => 'lanmps ', 'password' => '123')]
- * @ Param $ field value to be queried by fields [example: 'name', 'gender', 'birthday']
- * @ Param $ limit return result range [example: 10 or 10, 10 is blank by default]
- * @ Param $ order: Sort by database by default.
- * @ Param $ group: [null by default]
- * @ Return array number Group of query result sets
- */
- Public static function select ($ table, $ where = '', $ fields = '*', $ limit ='', $ order = '', $ group = ''){
- $ Db = self: cdb ();
- $ Db-> setTableName ($ table );
- Return $ db-> select ($ where, $ fields, $ limit, $ order, $ group );
- }
- /**
- * Query a single record
- * @ Param $ table name
- * @ Param array $ where query condition statement
- * String, in the following format:
- * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
- * [Example: array ('name' => 'lanmps ', 'password' => '123')]
- * @ Param string $ field value to be queried by fields [example: 'name', 'gender', 'birthday']
- * @ Param string $ order: [sort by database by default]
- * @ Param string $ group: [null by default]
- * @ Return array/null: query result set. If no result exists, null is returned.
- */
- Public static function getOne ($ table, $ where, $ fields = '*', $ order = '', $ group = ''){
- $ Db = self: cdb ();
- $ Db-> setTableName ($ table );
- Return $ db-> get_one ($ where, $ fields, $ order, $ group );
- }
- /**
- * Query multiple data entries and pagination
- * @ Param $ table name
- * @ Param $ where query condition
- * String, in the following format:
- * String [Example 1: "id = 1 and time> $ time"]
- * String [Example 2: array ('catid =: catid AND time >=: time', array (': catid' => 10 ,': time '=> '2017-02-10')]
- * [Example: array ('name' => 'lanmps ', 'password' => '123')]
- * @ Param $ fields field *, id
- * @ Param $ order sort id desc, orderlist asc
- * @ Param $ page number 1
- * @ Param $ pagesize the number of entries per page
- * @ Return array ('data' => data, 'count' => Total Records)
- */
- Public static function listInfo ($ table, $ where = '', $ fields = '*', $ order ='', $ page = 1, $ pagesize = 20 ){
- $ Db = self: cdb ();
- $ Db-> setTableName ($ table );
- $ D = $ db-> listinfo ($ where, $ fields, $ order, $ page, $ pagesize );
- Return array ('data' => $ d, 'count' => self: $ db-> number );
- }
- /** The first parameter value
- * @ Param $ SQL
- * @ Return mixed
- */
- Public static function resultFirst ($ SQL ){
- $ Db = self: cdb ();
- Return $ db-> resultFirst ($ SQL );
- }
- }
Call method: DB: insert ('test', array ('name' => 'test ')); |