New PDO database Operation class PHP version (MySQL only) _php instance

Source: Internet
Author: User
Tags dsn
Copy Code code as follows:

/**
* Author: Hu Rui
* Date: 2012/07/21
* Email: hooray0905@foxmail.com
*/

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; ');
To 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 Shutdown * *
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 debugging is turned on, then output SQL statement
* 0 does not open
* 1 Open
* 2 Open and terminate the program
* int $mode return type
* 0 return multiple records
* 1 return a single record
* 2 return number of rows
* String/array $table database tables, two types 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, allow nulls, default to find all, two mode of value transfer
* Normal Mode:
* ' Username, password '
* Array Mode:
* Array (' username ', ' password ')
* String/array $sqlwhere query conditions, allow NULL, two mode of transfer value
* Normal Mode:
* ' and type = 1 and username like '%os% '
* Array Mode:
* Array (' type = 1 ', ' username like '%os% ')
* String $orderby sort, default to ID reverse
*/
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 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 debugging is turned on, then output SQL statement
* 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 tables, two types of value-transfer modes
* Normal Mode:
* ' Tb_member, Tb_money '
* Array Mode:
* Array (' tb_member ', ' Tb_money ')
* String/array $set The fields and contents that need to be inserted, two modes of transfer 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 processing
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 debugging is turned on, then output SQL statement
* 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 type of value-transfer mode
* Normal Mode:
* ' Tb_member, Tb_money '
* Array Mode:
* Array (' tb_member ', ' Tb_money ')
* String/array $set need to update the field 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 the condition, allow null, two mode of transfer 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 processing
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 debugging is turned on, then output SQL statement
* 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 processing
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
}
}
}
}

In fact, use, and before the difference is not small, the purpose is to facilitate the transplant.

This rewrite focuses on a few issues:

①insert statement is too complex, fields and values are prone to error

Let's look at one of the most common SQL INSERT statements

Copy Code code as follows:
INSERT into Tb_member (username, type, dt) VALUES (' Test ', 1, now ())

In traditional mode, the fields and values parameters are passed in separately, but ensure that the parameters are passed in the same order. This can easily lead to a sequence disorder or an omission of a parameter.

This time, the problem has been modified, the use of MySQL unique insert syntax, the same is the above function, you can change this way

Copy Code code as follows:
INSERT INTO Tb_member set username = ' Test ', type = 1, Lastlogindt = Now ()

Just like update, at a glance.

② partial parameters can be replaced with arrays

Like such a SQL

Copy Code code as follows:
Delete from Tb_member where 1=1 and tbid = 1 and username = "Hooray"

When the method was originally called, it was necessary to manually assemble the where condition so that the operation was costly, and it is now fully available in this form
Copy Code code as follows:

$where = Array (
' Tbid = 1 ',
' username = ' Hooray '
);
$db->delete (1, 0, ' Tb_member ', $where);

No amount of conditions will disturb your thinking. Also, not only the where parameter, but also the set in update can be in this form (see full source)

Copy Code code as follows:

$set = Array (' username = 123 ', ' type = 1 ', ' Lastlogindt = Now () ');
$where = Array (' tbid = 1 ');
$db->update (1, 0, ' Tb_member ', $set, $where);

③ can customize SQL statements

Sometimes SQL is too complex to be able to assemble SQL statements using the methods provided in the class, which requires a function that can be passed directly into the SQL statement I've assembled and return the information. Now, this feature also has a

Copy Code code as follows:

$db->query (' Select username, password from tb_member ');
$rs = $db->fetchall ();

Is it like the PDO of the original ecology?

④ support for creating multiple database connections

Originally because only the database operation method, so does not support the multiple database connection, in the implementation needs to duplicate 2 identical files, modifies some variables, the operation is complex. Now the problem has been solved.

Copy Code code as follows:

$db _hoorayos_config = Array (
' DSN ' => ' Mysql:host=localhost;dbname=hoorayos ',
' Name ' => ' root ',
' Password ' => ' Hooray '
);
$db = new Hrdb ($db _hoorayos_config);

$db _hoorayos_config2 = Array (
' DSN ' => ' Mysql:host=localhost;dbname=hoorayos2 ',
' Name ' => ' root ',
' Password ' => ' Hooray '
);
$db 2 = new Hrdb ($db _hoorayos_config2);

This allows you to create 2 database connections at the same time to facilitate the processing of database and database interactions.

The general new function is so much, the whole code is not much, welcome to read the understanding. The following is the test code that I write when writing, also provides to come up, facilitates everybody to study.

Copy Code code as follows:

Require_once (' global.php ');
Require_once (' inc/setting.inc.php ');

$db = new Hrdb ($db _hoorayos_config);

Echo ' Echo ' Normal mode, direct string incoming <br> ';
$rs = $db->select (1, 0, ' tb_member ', ' username, password ', ' and type = 1 and username like "%os%");
echo ' <br> array mode, which can be passed into array <br> ';
$fields = Array (' username ', ' password ');
$where = Array (' type = 1 ', ' username like '%os% ');
$rs = $db->select (1, 0, ' Tb_member ', $fields, $where);

Echo ' Echo ' Normal mode, direct string incoming <br> ';
$db->insert (1, 0, ' tb_member ', ' username = ' Test ', type = 1, Lastlogindt = Now () ');
echo ' <br> array mode, which can be passed into array <br> ';
$set = Array (' username = ' test ', ' type = 1 ', ' Lastlogindt = Now () ');
$db->insert (1, 0, ' Tb_member ', $set);

Echo ' Echo ' Normal mode, direct string incoming <br> ';
$db->update (1, 0, ' tb_member ', ' username = ' 123 ', type = 1, Lastlogindt = Now () ', ' and tbid = 7 ');
echo ' <br> array mode, which can be passed into array <br> ';
$set = Array (' username = 123 ', ' type = 1 ', ' Lastlogindt = Now () ');
$where = Array (' tbid = 1 ');
$db->update (1, 0, ' Tb_member ', $set, $where);

Echo ' Echo ' Normal mode, direct string incoming <br> ';
$db->delete (1, 0, ' tb_member ', ' and tbid = 1 and username = ' hooray ');
echo ' <br> array mode, which can be passed into array <br> ';
$where = Array (
' Tbid = 1 ',
' username = ' Hooray '
);
$db->delete (1, 0, ' Tb_member ', $where);

Echo ' $db->query (' Select username, password from tb_member ');
$rs = $db->fetchall ();
Var_dump ($RS);

$db->close ();

Author: Hu Yuan
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.