-
- /**
- * Class DB
- * Database operation class
- */
- class DB {
- /**
- * @var
- * @return CDB
- */
- private static $db;
- /**Get CDb class
- * @param $table_name table name
- * @param string $db_setting call 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 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);
- }
- /**
- * Execute deletion record operation
- * @param $table table name
- * @param $condition Delete data condition, not allowed to be empty. Can be an array
- * @return boolean
- * /
- public static function delete($table, $condition) {
- $db=self::cdb();
- $db->setTableName($table);
- return $db->delete($condition);
- }
- /**
- * Execute adding record operation
- * @param $table 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 add data by replace into
- * @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);
- }
- /**
- * Get the primary key number of the last added record
- * @return int
- */
- public static function insertID() {
- $db=self::cdb();
- return $db->insert_id ();
- }
- /**
- * Execute update record operation
- * @param $table table name
- * @param $data The data content to be updated, the parameter is an array
- * When it is an array, the array key is the field value, and the array value is the data value
- * When it is an array [Example: array('name'=>'lanmps','password'=>'123456')]
- * Another way to use array array('name'=>'+=1', ' base'=>'-=1');The program will automatically parse as `name` = `name` + 1, `base` = `base` - 1
- * string, please follow the format:
- * string[ Example 2: array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * @param $where conditions for updating data,
- * string, please follow the format:
- * string [Example 1: " id=1 and time>$time " ]
- * string [Example 2: array('catid=: catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * Array [Example: array('name'= >'lanmps','password'=>'123456')]
- * @return boolean
- */ bbs.it-home.org
- public static function update($table, $data, $where) {
- $db=self::cdb();
- $ db->setTableName($table);
- return $db->update($data,$where);
- }
- /**
- * Get single record query
- * @param array $sql query condition statement
- * @return array/null data query result set, if it does not exist, return empty
- */
- public static function fetchFirst($sql) {
- $db =self::cdb();
- return $db->fetch($sql);
- }
- /**
- * Execute sql query
- * @param $sql query conditions
- * @return array Query result set array
- */
- public static function fetchAll($sql) {
- $db=self::cdb ();
- return $db->fetchAll($sql);
- }
- /**
- * Directly execute sql query
- * @param $sql Query sql statement
- * @return
- */
- public static function query($sql) {
- $db=self::cdb();
- return $db->exec($sql);
- }
- /**
- * Execute sql query
- * @param $table table name
- * @param $where query condition
- * String, please follow the format:
- * String [Example 1: " id=1 and time>$time " ]
- * String [Example 2: array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * Array [Example: array('name'=>'lanmps','password'=>'123456')]
- * @param $fields Field values to be queried [Example `name`, `gender` ,`birthday`]
- * @param $limit Return result range [Example: 10 or 10,10 is empty by default]
- * @param $order Sorting method [Default is sorted by the database default method]
- * @param $group Grouping method [Default is empty]
- * @return array Query result set array
- */
- public static function select($table,$where = '', $fields = '*', $limit = '', $order = '', $group = '') {
- $db=self::cdb();
- $db->setTableName($table);
- return $db->select($where , $fields , $limit, $order, $group);
- }
- /**
- * Get single record query
- * @param $table table name
- * @param array $where query conditional statement
- * String, please follow the format:
- * String [Example 2: array('catid=:catid AND time> ;=:time ',array(':catid'=>10,':time'=>'2012-02-10')) ]
- * Array [Example: array('name'=>' lanmps','password'=>'123456')]
- * @param string $fields Field values to be queried [eg `name`,`gender`,`birthday`]
- * @param string $order Sorting method[ The default is to sort according to the database default method]
- * @param string $group grouping method [default is empty]
- * @return array/null data query result set, if it does not exist, return empty
- */
- 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 pieces of data and paginate
- * @param $table table name
- * @param $where query conditions
- * String, please follow the format:
- * String [Example 1: " id=1 and time>$time " ]
- * String [Example 2: array('catid=:catid AND time>=:time ',array(':catid'=>10,':time'=>'2012-02-10' )) ]
- * Array [Example: array('name'=>'lanmps','password'=>'123456')]
- * @param $fields field*,id
- * @param $order sort id desc ,orderlist asc
- * @param $page page number 1
- * @param $pagesize number of items per page
- * @return array('data'=>data,'count'=>total number of 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);
- }
- /**First parameter value
- * @param $sql
- * @return mixed
- */
- public static function resultFirst($sql){
- $db=self::cdb();
- return $db->resultFirst($sql);
- }
- }
复制代码
调用方法:
DB::insert('test',array('name'=>'test')); |