PHP database operations based on MYSQLI database Operation class Library _php skills

Source: Internet
Author: User
Tags php database
This class library is simple, easy to use, easy to modify and improve your functionality, and can solve the SQL operations that are performed in most PHP projects.

Preliminary work

First of all, please download this class library m.class.php and then download a mysqli to connect the database class library MysqliDb.class.php (package download address)
Create a new includes folder and put the downloaded two class files in.
Then, please create a test.php file under the project. Note: UTF-8 file format

Please fill in the following code to connect to the database according to your machine condition:

Copy Code code as follows:

Header (' content-type:text/html; Charset=utf-8 ');
Define (' Root_path ', DirName (__file__));
Define (' db_host ', ' localhost '); Database server address
Define (' Db_user ', ' root '); Database user Name
Define (' db_pwd ', ' xxx ');//Database Password
Define (' db_name ', ' xxx '); Database name
Define (' Db_port ', ' 3306 '); Database port
function __autoload ($className) {
Require_once Root_path. '/includes/'. Ucfirst ($className). Class.php '; Load class file automatically
}

Well, the above operations are preliminary work, the following formal access to the class library explanation.

Class Library Tutorial

First, we have to instantiate the m.class.php, and the instantiation is simple:

Copy Code code as follows:

$m = new M (); This step represents all of the features in the m.class.php encapsulated in the variable $m

Note:
1, M class library in the method parameter description, please go to the m.class.php file to see detailed comments, no longer described here. It is suggested that when you study, check the parameters in the file as comments.
2, to explain the code used in the database structure:

Copy Code code as follows:

CREATE TABLE ' user ' (
' ID ' int (8) unsigned not NULL auto_increment,
' name ' varchar default NULL,
' Email ' varchar (MB) default NULL,
' Age ' smallint (3) default NULL,
' class_id ' int (8) Default NULL,
' Commit_time ' int (a) default NULL,
PRIMARY KEY (' id '),
KEY ' name ' (' name ')
) Engine=myisam DEFAULT Charset=utf8

Copy Code code as follows:

CREATE TABLE ' class ' (
' class_id ' int (8) not NULL auto_increment,
' class_name ' varchar default NULL,
PRIMARY KEY (' class_id ')
) Engine=innodb DEFAULT Charset=utf8

and add a test data.

3, M class library, most of the methods are divided into two types, namely: SQL method, splicing method, concrete in the example can be seen
4. The M in the following reference is m.class.php file

Method 1, Insert () Add data

The full use cases for the Insert method are as follows:

Copy Code code as follows:

$m->insert ("user", NULL, Array (' Coke ', ' liruxing1715@sina.com ', ') ', Time ()); Stitching method: Add a piece of data to the ' user ' table and return the number of rows that the value is affected by the database
$m->insert ("user", NULL, Array (' focal char ', ' liruxing1715@sina.com ', ', '), Time ()), true); Function ditto, return last_insert_id (insert growth ID)
$m->insert ("Insert into ' user" (' name ', ' email ', ' age ', ' Commit_time ') VALUES (' Zhang Floret ', ' zhangxiaohua@sina.com.cn ', ') ", '". Time (). "')"; SQL method that returns the number of rows affected by the value of the database
$m->insert ("Insert into ' user" (' name ', ' email ', ' age ', ' Commit_time ') VALUES (' Zhang Floret ', ' zhangxiaohua@sina.com.cn ', ') ", '". Time (). "')", true); Ditto, return to last_insert_id

Note: If the second argument in the Insert method is null, you can automatically get all fields except the Auto_increment field inserted into the table, see the M source file; To return the ID that was last inserted, set the last parameter of the Insert method to True (default is false);

Method 2, Update () modify data

The full use cases for the Update method are as follows:

Copy Code code as follows:

$m->update ("User", Array (' name ' => ' Lijuju ', ' age ' =>24), "id=1"); Concatenation method, the name of the data with ID 1 is "Lijuju"; age is "24", and the return value of the method is the number of rows affected
$m->update ("Update ' user ' SET ' name ' = ' Lijuju ', ' age ' =24 WHERE id = 1"); SQL usage, Function ditto

Method 3, Del () Delete data

The full use case for the Del method is as follows:

Copy Code code as follows:

$m->del (' user ', ' id=3 '); Stitching method: Delete data with ID 3 in the ' User ' table, and return the number of rows affected
$m->del ("DELETE from ' user ' WHERE id=4"); SQL method: Deletes the data with ID 4 in the ' User ' table and returns the number of rows affected
$m->del ("DELETE from ' user ' WHERE ID is in (10, 11, 12)"); SQL method: Delete multiple data, delete data in the ' user ' table with IDs 10, 11, 12, and return the number of rows affected

Method 4, Total () Gets the number of records, the return value is int

The full use case for the Del method is as follows:

Copy Code code as follows:

$m->total (' user '); Stitching method: Returns the number of records in the ' User ' table, unconditional
$m->total (' user ', ' id>1 '); Stitching method: Returns the number of records in the ' User ' table with IDs greater than 1, conditional
$m->total ("Select COUNT (*) as total from ' user '"); SQL method, note: Using the SQL method, the statement must use "as total", otherwise it will be an error

Method 5, Isexists () to check whether the data exists, the return value is Boolean

Copy Code code as follows:

$m->isexists (' user ', ' ' name ' = ' focal char '); Stitching method: Returns True if there is a ' name ' in the ' user ' table and returns False if it does not exist

Method 6, Insertid () Gets the next automatic growth ID that is added to the table, note that no additions are made here, just get the next growth ID

Copy Code code as follows:

echo $m->insertid (' user '); Get the next automatic growth ID that is added to the ' User ' table

Method 7, GetRow () returns a single data, the return value is a one-dimensional array

The full use case for the GetRow method is as follows:

Copy Code code as follows:

$data = $m->getrow ("Select ' name ', email from ' user ' WHERE id=1"); SQL method, which returns a one-dimensional array, for example: Array ([name] => Joe. [email] => liruxing1715@sina.com)
$data = $m->getrow ("Select U. ' Name", U.email, c.class_name from ' User ' U, ' class ' C WHERE u.class_id=c.class_id and U.I  D=1 "); SQL methods, multiple table queries
$data = $m->getrow (' user ', ' name ', email ', ' id=1 '); Stitching method
$data = $m->getrow (' User as U, ' class ' C ', ' U. ' name ', U.email,c.class_name ', ' u.id=1 and u.class_id=c.class_id '); Stitching method, multiple table query
$data = $m->getrow ("Select ' name ', email from ' user '"); If you do not specify a condition, you should display all the information, but the first article is displayed by default in this method (this is not recommended for use!!! )

$data is a one-dimensional array of queries.

Method 8, GetOne () returns a single data

The full use case for the GetOne method is as follows:

Copy Code code as follows:

$name = $m->getone ("Select ' Name ' from ' user ' WHERE id=1"); SQL method that returns a string, for example: Joe.
$name = $m->getone ("User", "name", "id=1"); A concatenation method that returns a string, for example: Joe.

Method 9, Fetchall () returns all records

Copy Code code as follows:

$data = $m->fetchall ("user"); Returns all records in the ' User ' table, in the form of a two-dimensional array
$data = $m->fetchall ("SELECT * from ' user '"); SQL method, function and return value Ibid
$data = $m->fetchall ("User", "Name,email", "id>1", ' ID DESC ', ' 2 '); Returns two id>1 data, displays only Name,email, and sorts in reverse order of ID. Note: Please note that the last parameter of the method can also be ' 0,2 ', which is intended for paging, if the first page is ' 0,2 ', then the second page is ' 2,2 '
This method also supports both table query and multiple table query, with the following table query as an example
$data = $m->fetchall ("' User ' as U left JOIN ' class ' as C on u.class_id=c.class_id", "U. ' Name ', U.email, C.class_name", " U.id=1 "); Note: In this stitching method, on the added location


Note: For the Fetchall method, I will write an article using this method to finish the penny page!! Please pay attention.

Method 10, Multiquery () Execute multiple SQL statements

Copy Code code as follows:

$sql = "INSERT into user (' name ', email, age, class_id, Commit_time) VALUES (' Jia ', ' jiahuahua@sina.com.cn ', ' 22 ', ' 1 ', '".) Time (). "')"; Add a student's message called "Jia"
$sql. = ";D elete from ' user ' WHERE ' name ' = ' focal char '"; Delete A student's information called "Coke"
Explanation: $sql is more than one SQL in English; (semicolon) spliced together
$data = $m->multiquery ($sql); Returns true to represent successful execution;

Class Library Tutorial finished

All the functions of this class library are explained, I hope you can look at the M file and understand the mechanism of its internal operation. M file does not exist slow execution, please rest assured that use.
If a SQL concatenation error occurs during use, the class library will report a friendly error message.
Please leave a message, you are welcome to criticize and suggestions, come on! Learn good luck.

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.