- /**
- * 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);
- }
- /**
- * Perform Delete record operation
- * @param $table Table name
- * @param $condition Delete data conditions, not allowed to empty. You can set an array
- * @return Boolean
- */
- public static function Delete ($table, $condition) {
- $db =self::cdb ();
- $db->settablename ($table);
- Return $db->delete ($condition);
- }
- /**
- * Perform add 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 using 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);
- }
- /**
- * Gets the primary key number of the last record added
- * @return int
- */
- public static function Insertid () {
- $db =self::cdb ();
- return $db->insert_id ();
- }
- /**
- * Perform update record operation
- * @param $table Table name
- * @param $data The data content to be updated, the parameter is an array
- * Array key is a field value, array value is data value
- * array When [Example: Array (' name ' = ' lanmps ', ' password ' = ' 123456 ')]
- * An array of another use array (' name ' = ' = ' +=1 ', ' base ' = '-=1 '); the program automatically resolves to ' 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 when 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 When [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 a single record query
- * @param array $sql query condition statement
- * @return array/null data Query result set, if not present, returns an 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
- */
- public static function Fetchall ($sql) {
- $db =self::cdb ();
- Return $db->fetchall ($sql);
- }
- /**
- * Execute SQL query directly
- * @param $sql Query SQL statements
- * @return
- */
- public static function query ($sql) {
- $db =self::cdb ();
- Return $db->exec ($sql);
- }
- /**
- * Execute SQL query
- * @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 When [Example: Array (' name ' = ' = ' lanmps ', ' password ' = ' 123456 ')]
- * @param $fields field values to query [example ' name ', ' gender ', ' birthday ']
- * @param $limit return result range [Example: 10 or 10, 10 default is empty]
- * @param $order Sort By default by database
- * @param $group grouping method [default is empty]
- * @return Array Query result set
- */
- 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 a single record query
- * @param $table Table name
- * @param array $where query condition statement
- * String, please follow the format:
- * String [Example 2:array (' Catid=:catid and Time>=:time ', Array (': CatID ' =>10, ': Time ' = ' 2012-02-10 '))]
- * array When [Example: Array (' name ' = ' = ' lanmps ', ' password ' = ' 123456 ')]
- * @param string $fields The field value to query [example ' name ', ' gender ', ' birthday ']
- * @param string $order Sort by default by database
- * @param string $group grouping method [default is empty]
- * @return array/null data Query result set, if not present, returns an empty
- */
- public static function GetOne ($table, $where, $fields = ' * ', $order = ', $group = ') {
- $db =self::cdb ();
- $db->settablename ($table);
- Return $db->get_one ($where, $fields, $order, $group);
- }
- /**
- * Querying multiple data and paging
- * @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 When [Example: Array (' name ' = ' = ' lanmps ', ' password ' = ' 123456 ')]
- * @param $fields Field *,id
- * @param $order Sort ID desc, orderlist ASC
- * @param $page Page 1
- * @param $pagesize number of pages per page
- * @return Array (' data ' = = The total number of records, ' count ')
- */
- 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);
- }
- }
Copy CodeCall Method: Db::insert (' Test ', Array (' name ' = ' test ')); |