- Class hrdb{
- protected $pdo;
- protected $res;
- protected $config;
- /* Constructor function */
- 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; ');
- Serialize the results into Stdclass
- $this->pdo->setattribute (Pdo::attr_default_fetch_mode, pdo::fetch_obj);
- Write your own code capture exception
- $this->pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception);
- }
- /* Database off */
- 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 turn on debug, output SQL statement on
- * 0 does not open
- * 1 Open
- * 2 Open and terminate the program
- * int $mode return type
- * 0 returns multiple records
- * 1 Returns a single record
- * 2 Number of rows returned
- * String/array $table database table, two kinds of value-transfer modes
- * Normal Mode:
- * ' Tb_member, Tb_money '
- * Array Mode:
- * Array (' tb_member ', ' Tb_money ')
- * String/array $fields database fields that need to be queried, allowed to be empty, default to find all, two modes of pass value
- * Normal Mode:
- * ' Username, password '
- * Array Mode:
- * Array (' username ', ' password ')
- * String/array $sqlwhere query condition, allow NULL, two modes of transmission value
- * Normal Mode:
- * ' and type = 1 and username like "%os%" '
- * Array Mode:
- * Array (' type = 1 ', ' username like '%os% "')
- * String $orderby Sort, default to reverse ID
- */
- Public Function Select ($debug, $mode, $table, $fields = "*", $sqlwhere = "", $orderby = "Tbid desc") {
- Parameter handling
- if (Is_array ($table)) {
- $table = Implode (', ', $table);
- }
- if (Is_array ($fields)) {
- $fields = Implode (', ', $fields);
- }
- if (Is_array ($sqlwhere)) {
- $sqlwhere = ' and '. Implode (' and ', $sqlwhere);
- }
- Database operations
- 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 turn on debug, output SQL statement on
- * 0 does not open
- * 1 Open
- * 2 Open 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 kinds of value-transfer modes
- * Normal Mode:
- * ' Tb_member, Tb_money '
- * Array Mode:
- * Array (' tb_member ', ' Tb_money ')
- * String/array $set need to insert the field and content, two modes of transmission value
- * Normal Mode:
- * ' username = ' Test ', type = 1, dt = Now () '
- * Array Mode:
- * Array (' username = ' test ' ', ' type = 1 ', ' dt = Now () ')
- */
- Public Function Insert ($debug, $mode, $table, $set) {
- Parameter handling
- if (Is_array ($table)) {
- $table = Implode (', ', $table);
- }
- if (Is_array ($set)) {
- $set = Implode (', ', $set);
- }
- Database operations
- 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 turn on debug, output SQL statement on
- * 0 does not open
- * 1 Open
- * 2 Open and terminate the program
- * int $mode return type
- * 0 no return information
- * 1 Returns the number of execution entries
- * String $table database table, two modes of transfer value
- * Normal Mode:
- * ' Tb_member, Tb_money '
- * Array Mode:
- * Array (' tb_member ', ' Tb_money ')
- * String/array $set need to update the fields and content, two types of value-transfer mode
- * Normal Mode:
- * ' username = ' Test ', type = 1, dt = Now () '
- * Array Mode:
- * Array (' username = ' test ' ', ' type = 1 ', ' dt = Now () ')
- * String/array $sqlwhere Modify condition, allow null, two modes of transmission value
- * 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 = "") {
- Parameter handling
- if (Is_array ($table)) {
- $table = Implode (', ', $table);
- }
- if (Is_array ($set)) {
- $set = Implode (', ', $set);
- }
- if (Is_array ($sqlwhere)) {
- $sqlwhere = ' and '. Implode (' and ', $sqlwhere);
- }
- Database operations
- 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 turn on debug, output SQL statement on
- * 0 does not open
- * 1 Open
- * 2 Open 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 Delete condition, allow null, two modes of transfer value
- * Normal Mode:
- * ' and type = 1 and username like "%os%" '
- * Array Mode:
- * Array (' type = 1 ', ' username like '%os% "')
- */
- Public Function Delete ($debug, $mode, $table, $sqlwhere = "") {
- Parameter handling
- if (Is_array ($sqlwhere)) {
- $sqlwhere = ' and '. Implode (' and ', $sqlwhere);
- }
- Database operations
- 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
- }
- }
- }
- }
Copy Code
|