Mysql database operations implemented by PHP-php Tutorial

Source: Internet
Author: User
Mysql database operations implemented by PHP are provided by the trace Yi blog

Db Class introduction

1. the Singleton design mode is adopted. The Singleton design mode ensures the unique implementation of the instance objects.

Public static $ _ instance; // static attribute, which stores instance objects
/*** Private constructor this is a required step to construct the Singleton design mode */private function _ construct ($ config = '') {$ this-> config = $ this-> parseConfig ($ config );}
/*** The instantiated object adopts the static public method */public static function Instance () {if (self ::$ _ instance instanceof self) {return self ::$ _ instance ;} self: $ _ instance = new self; return self: $ _ instance ;}

2. Features

First, this class supports the connection of the master-slave replication database, and supports the database in one master-slave multi-slave mode.

When a thread is connecting to a slave server down, the program will automatically reconnect to other normal slave servers

When new data is added to a data table, this class supports adding multiple data records at a time, and the data records can be different tables.

You can use the second parameter of the addMore function to specify multi-table insertion. the core code is as follows:

/*** Insert multiple data records at a time, supports insertion of different tables * when using the multi-table insertion function, you must specify $ options ['multitable'] = true * in the second parameter and the $ data format is * array (* 'Table name 1' => array (), array (), * 'table name 2' => array (), array ())*) * @ param array $ data * @ param array $ options * @ return boolean */public function addMore ($ data = array (), $ options = array ()) {if (isset ($ options ['table']) $ this-> table ($ options ['table']); if (! Is_array ($ data) return false;/** enable multiple statements for transaction processing */$ this-> startTransaction (); foreach ($ data as $ key => $ val) {// check whether multiple tables are inserted. if (isset ($ options ['multitable']) & $ options ['multitable']) {/** multi-table insertion, then $ key is the table name, and $ val is the data to be inserted * multiple data records are inserted recursively */$ res = $ this-> addMore ($ val, array ('table' => $ key);} else {// Insert $ res = $ this-> add ($ val);} if (! $ Res) {// if one data insertion fails, the transaction is rolled back and all operations $ this-> rollback (); return false ;}} are canceled ;}} // if all the insert operations are correct, commit the transaction $ this-> commit (); return true ;}

Transaction processing is also supported. when one of multiple inserted data entries fails to be inserted, you can cancel other inserted data through transaction rollback.

3. other support for normal addition, deletion, modification, and query

The following describes the usage of the functions used.

1) instantiate this object

To use this class, you must first instantiate the object of this class.

$obj = Db::Instance();

2) search for data

The functions used to query data include select () and find ().

The select () function searches for multiple data records.

Use instance

$res = $obj->field('id,name')->where('id > 10')->select();

Return value:

If the query fails, false is returned. if the query succeeds, multiple data entries are returned.

Array ('id' => 11, 'name' => 'recall blog 1'), array ('id' => 12, 'name' => 'recalling blog 2 '),)

Find () returns a piece of data.

$res = $obj->field('id,name')->where('id=10')->find()

Return value

If the query fails, false is returned. if the query succeeds, a data record is returned.

Array ('id' => 10, 'name' => 'recall blog ')

3) add data

Add ($ data, $ options) and addMore ($ data, $ options) functions to add data)

add($data.$options)

$ Data the data to be added

Data format

array('id'=>13,'name'=>'onmpw')

$ Options

You can specify the table name format as array ('table' => 'table name'). The table name has the highest priority.

Return value

If insertion fails, false is returned. If insertion succeeds, the number of inserted items is returned.

addMore($data,$options)

You can use the $ options option to specify whether to insert a single table or not.

'Multitable' => true multi-table insertion if this option is set, the default value is single-table insertion multi-table insertion $ data format $ data = array ('tablename1' => array ('id' => 20, 'name' => 'recall blog 1'), array ('id' => 21, 'name' => 'recall blog 2 '), array ('id' => 22, 'name' => 'recall blog 3'), 'tablename2' => array ('id' => 20, 'name' => 'recalling blog 1', 'URL' => 'Www .onmp;com '), array ('id' => 21, 'name' => 'recalling blog 2', 'URL' => 'Www .onmp;com '), array ('id' => 22, 'name' => 'recalling blog 3', 'URL' => 'Www .onmp;com ')))
'Multitable' => false/the data format of $ data inserted to this single table is not set to $ data = array ('id' => 31, 'name' => 'recalling blog 1', 'URL' => 'Www .onmp;com '), array ('id' => 32, 'name' => 'recalling blog 2', 'URL' => 'Www .onmp;com '), array ('id' => 33, 'name' => 'recalling blog 3', 'URL' => 'Www .onmp;com '),)

'Table' => 'table name' specifies the name of the data table to be inserted. this parameter is valid when a single table is inserted and has a higher priority than other specified table names.

Return value

If insertion fails, false is returned. If insertion succeeds, the number of inserted items is returned.

4) modify update ($ data, $ options)

Modify data functions

$ Data: the data to be modified. the format is

array('name'=>'onmpw','url'=>'http://onmpw.com');

$ Options: specifies the table name.

'Table' => 'table name'

Return value

If the update fails, false is returned. if the update succeeds, the number of updated items is returned.

5) delete ($ options)

$ Options: specifies the table name.

'Table' => 'table name' specifies the highest priority of a table name.

Instance

$ Res = $ obj-> table ('repl')-> where ('Id = 10')-> delete (); // delete records with id = 10 in the repl table $ res = $ obj-> table ('repl')-> where ('Id = 13 ') -> delete (array ('table' => 'test'); // delete records with id = 13 in the test table

Equivalent

$res = $obj->where('id=13')->delete(array('table'=>'test'))

Return value

If deletion fails, false is returned. if deletion succeeds, the number of deleted records is returned.

6) the table ($ str) function specifies the table name.

$ Obj-> table ('test') // specify the table for the current operation as the test table

The returned value is the current object.

7) where ($ where) specifies the where condition

$ Where can be a string or an array

String

$ Obj-> table ('test')-> where ("name = 'recall blog ', url = 'www .onmp;com '");

Array

$ Obj-> table ('test')-> where (array ('name' => 'recall blog', 'URL' => 'Www .onmp;com '))

The returned value is the current object.

8) field ($ field) specifies the name of the queried field

$obj->table('test')->field('name,url')->select();

If the field specified by the field () function is not applicable during query, all fields in the table are queried by default.

The returned value is the current object.

9) orderby ($ str) specifies the sort by field

$ Obj-> table ('test')-> field ('Id, name, URL')-> where ("name = 'recalling blog '") -> orderby ('Id DESC ')-> select ();

Sort by id in descending order

$ Obj-> table ('test')-> field ('Id, name, URL')-> where ("name = 'recalling blog '") -> orderby ('id')-> select ();

You can also choose not to specify descending or ascending

The returned value is the current object.

10) limit ($ limit)

$ Limt can be a string or an array

Array

array(page,listrows)

Page specifies the current page number. listrows specifies the number of entries Retrieved per page.

String

10, 12

10 indicates getting from the tenth record, and 12 indicates the number of retrieved records.

$ Res = $ obj-> table ('test')-> field ('Id, name, URL')-> where ("name = 'recall blog '") -> orderby ('Id desc')-> limit ('10, 12')-> select ()

The returned value is the current object.

11) SQL ($ SQL) executes the specified SQL statement

$ SQL = "select name, url from test where name = 'recall blog'"; $ res = $ obj-> SQL ($ SQL );

Returns the execution result.

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.