-
- class HRDB{
- protected $pdo;
- protected $res;
- protected $config;
-
- /*constructor*/
- function __construct($config){
- $this->Config = $config;
- $this->connect();
- }
-
- /*Database connection*/
- public function connect(){
- $this->pdo = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password']);
- $this->pdo->query('set names utf8;');
- //Put the result Serialized into stdClass
- //$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
- //Write your own code to catch Exception
- $this->pdo->setAttribute(PDO: :ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
-
- /*Database close*/
- public function close(){
- $this->pdo = null;
- }
-
- public function query($sql){
- $ res = $this->pdo->query($sql);
- if($res){
- $this->res = $res;
- }
- }
- public function exec($sql){
- $ res = $this->pdo->exec($sql);
- if($res){
- $this->res = $res;
- }
- }
- public function fetchAll(){
- return $this ->res->fetchAll();
- }
- public function fetch(){
- return $this->res->fetch();
- }
- public function fetchColumn(){
- return $this-> ;res->fetchColumn();
- }
- public function lastInsertId(){
- return $this->res->lastInsertId();
- }
-
- /**
- * Parameter Description
- * int $debug Whether to enable debugging, if enabled, the sql statement will be output
- * 0 Do not enable
- * 1 Enable
- * 2 Enable and terminate the program
- * int $mode Return type
- * 0 Return multiple records
- * 1 Returns a single record
- * 2 Returns the number of rows
- * string/array $table database table, two value-passing modes
- * Normal mode:
- * 'tb_member, tb_money'
- * Array mode:
- * array('tb_member', 'tb_money')
- * string/array $fields Database fields to be queried, allowed to be empty, default is to find all, two value-passing modes
- * Normal mode:
- * 'username, password'
- * Array mode:
- * array('username', 'password')
- * string/array $sqlwhere query conditions, empty allowed, two value-passing modes
- * Normal mode:
- * 'and type = 1 and username like "%os%"'
- * Array mode:
- * array('type = 1', 'username like "%os%"')
- * string $orderby sorting, the default is id reverse order
- */
- public function select( $debug, $mode, $table, $fields="*", $sqlwhere="", $orderby="tbid desc"){
- //Parameter processing
- if(is_array($table)){
- $table = implode(', ', $table);
- }
- if(is_array($fields)){
- $fields = implode(', ', $fields);
- }
- if(is_array($sqlwhere)){
- $ sqlwhere = ' and '.implode(' and ', $sqlwhere);
- }
- //Database operation
- if($debug === 0){
- if($mode === 2){
- $this-> ;query("select count(tbid) from $table where 1=1 $sqlwhere");
- $return = $this->fetchColumn();
- }else if($mode === 1){
- $this ->query("select $fields from $table where 1=1 $sqlwhere order by $orderby");
- $return = $this->fetch();
- }else{
- $this->query( "select $fields from $table where 1=1 $sqlwhere order by $orderby");
- $return = $this->fetchAll();
- }
- return $return;
- }else{
- if($mode = == 2){
- echo "select count(tbid) from $table where 1=1 $sqlwhere";
- }else if($mode === 1){
- echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
- }
- else{
- echo "select $fields from $table where 1=1 $sqlwhere order by $orderby";
- }
- if($debug === 2){
- exit;
- }
- }
- }
-
- /**
- * Parameter Description
- * int $debug Whether to enable debugging, if enabled, the sql statement will be output
- * 0 Do not enable
- * 1 Enable
- * 2 Enable and terminate the program
- * int $mode Return type
- * 0 No return information
- * 1 Returns the number of execution entries
- * 2 Returns the id of the last inserted record
- * string/array $table database table, two value-passing modes
- * Normal mode:
- * 'tb_member, tb_money'
- * Array mode:
- * array( 'tb_member', 'tb_money')
- * string/array $set Fields and contents to be inserted, two value-passing modes
- * Normal mode:
- * 'username = "test", type = 1, dt = now() '
- * Array mode:
- * array('username = "test"', 'type = 1', 'dt = now()')
- */
- public function insert($debug, $mode, $table, $set){
- //参数处理
- if(is_array($table)){
- $table = implode(', ', $table);
- }
- if(is_array($set)){
- $set = implode(', ', $set);
- }
- //数据库操作
- if($debug === 0){
- if($mode === 2){
- $this->query("insert into $table set $set");
- $return = $this->lastInsertId();
- }else if($mode === 1){
- $this->exec("insert into $table set $set");
- $return = $this->res;
- }else{
- $this->query("insert into $table set $set");
- $return = NULL;
- }
- return $return;
- }else{
- echo "insert into $table set $set";
- if($debug === 2){
- exit;
- }
- }
- }
-
- /**
- * Parameter Description
- * int $debug Whether to enable debugging, if enabled, the sql statement will be output
- * 0 Do not enable
- * 1 Enable
- * 2 Enable and terminate the program
- * int $mode Return type
- * 0 No return information
- * 1 Returns the number of execution entries
- * string $table database table, two value-passing modes
- * Normal mode:
- * 'tb_member, tb_money'
- * Array mode:
- * array('tb_member', 'tb_money')
- * string/ array $set Fields and contents that need to be updated, two value-passing modes
- * Normal mode:
- * 'username = "test", type = 1, dt = now()'
- * Array mode:
- * array('username = "test"', 'type = 1', 'dt = now()')
- * string/array $sqlwhere Modify the conditions, allow empty, two value-passing modes
- * Normal mode:
- * 'and type = 1 and username like "%os%"'
- * Array mode:
- * array('type = 1', 'username like "%os%"')
- */
- public function update($debug, $mode, $table, $set, $sqlwhere=""){
- //参数处理
- if(is_array($table)){
- $table = implode(', ', $table);
- }
- if(is_array($set)){
- $set = implode(', ', $set);
- }
- if(is_array($sqlwhere)){
- $sqlwhere = ' and '.implode(' and ', $sqlwhere);
- }
- //数据库操作
- if($debug === 0){
- if($mode === 1){
- $this->exec("update $table set $set where 1=1 $sqlwhere");
- $return = $this->res;
- }else{
- $this->query("update $table set $set where 1=1 $sqlwhere");
- $return = NULL;
- }
- return $return;
- }else{
- echo "update $table set $set where 1=1 $sqlwhere";
- if($debug === 2){
- exit;
- }
- }
- }
-
- /**
- * Parameter Description
- * int $debug Whether to enable debugging, if enabled, the sql statement will be output
- * 0 Do not enable
- * 1 Enable
- * 2 Enable and terminate the program
- * int $mode Return type
- * 0 No return information
- * 1 Returns the number of execution entries
- * string $table database table
- * string/array $sqlwhere deletion condition, allowed to be empty, two value-passing modes
- * Normal mode:
- * 'and type = 1 and username like "%os%" '
- * Array mode:
- * array('type = 1', 'username like "%os%"')
- */
- public function delete($debug, $mode, $table, $sqlwhere=""){
- //参数处理
- if(is_array($sqlwhere)){
- $sqlwhere = ' and '.implode(' and ', $sqlwhere);
- }
- //数据库操作
- if($debug === 0){
- if($mode === 1){
- $this->exec("delete from $table where 1=1 $sqlwhere");
- $return = $this->res;
- }else{
- $this->query("delete from $table where 1=1 $sqlwhere");
- $return = NULL;
- }
- return $return;
- }else{
- echo "delete from $table where 1=1 $sqlwhere";
- if($debug === 2){
- exit;
- }
- }
- }
- }
复制代码
|