PHP connects to the database to achieve the most basic addition, deletion, modification, and query (object-oriented)

Source: Internet
Author: User
Connecting to the database in PHP to achieve the most basic addition, deletion, modification, and query (process-oriented) This article has introduced how to connect PHP to the database and the most basic operations on the database.

Connecting to the database in PHP to achieve the most basic addition, deletion, modification, and query (process-oriented) This article has introduced the PHP database connection method and the most basic database operations, however, it does not implement modularization. all the code is concentrated on the presentation page, leading to code redundancy, which is not conducive to maintenance but also to code reusability, in this article, we will use object-oriented knowledge to encapsulate database connections and basic operation methods, which greatly avoids code duplication.

Next we will create a database operation class:

1. create the mysql_class.php file, create a Mysql class in the file, and define the variable

1234567891011

2. initialize the class through the constructor

1234567 Function _ construct ($ host, $ root, $ password, $ database) {$ this-> host = $ host; $ this-> root = $ root; $ this-> password = $ password; $ this-> database = $ database; $ this-> connect ();}

For the connect () method, next step

3. create a database connection and close the database

123456789 Function connect () {$ this-> conn = mysql_connect ($ this-> host, $ this-> root, $ this-> password) or die ("DB Connnection Error! ". Mysql_error (); mysql_select_db ($ this-> database, $ this-> conn); mysql_query ("set names utf8");} function dbClose () {mysql_close ($ this-> conn );}

4. encapsulate the mysql_query (), mysql_fetch_array (), and mysql_num_rows () functions.

1234567891011 Function query ($ SQL) {return mysql_query ($ SQL);} function myArray ($ result) {return mysql_fetch_array ($ result);} function rows ($ result) {return mysql_num_rows ($ result );}

5. Custom Data Query Methods

123 Function select ($ tableName, $ condition) {return $ this-> query ("SELECT * FROM $ tableName $ condition ");}

6. Custom Data insertion methods

123 Function insert ($ tableName, $ fields, $ value) {$ this-> query ("insert into $ tableName $ fields VALUES $ value ");}

7. Custom Data modification methods

123 Function update ($ tableName, $ change, $ condition) {$ this-> query ("UPDATE $ tableName SET $ change $ condition ");}

8. Custom Data deletion methods

123 Function delete ($ tableName, $ condition) {$ this-> query ("delete from $ tableName $ condition ");}

Now, the database operation class has been encapsulated. let's take a look at how to use it.

We still use PHP to connect to the database to achieve the most basic addition, deletion, modification, and query (process-oriented) of the databases and tables involved in the article (add data in the table yourself ):

9. instantiate the database operation class first.

1 $ Db = new Mysql ("localhost", "root", "admin", "beyondweb_test ");

Instantiation can be performed outside the Mysql class in the mysql_class.php file.

Then create a test. php file, first introduce the mysql_class.php file

123

Then let's get started.

10. Insert data into the table

1234 Insert ("user", "(nikename, email)", "(# beyondweb #, # beyondwebcn@xx.com #)"); // replace # with single quotes $ db-> dbClose ();?>

11. modify table data

1234 Update ("user", "nikename = # beyondwebcn #", "where id = #2 #"); // replace # with single quotes $ db-> dbClose ();?>

12. query and output table data

12345678910111213141516171819202122232425262728 Select ("user"); $ row = $ db-> rows ($ select); if ($ row >=1) {?> MyArray ($ select) {echo" "; Echo" "; // Replace # with the single quotation mark echo" "; // Replace # with the single quotation mark echo" "; // Replace # with the single quotation mark echo" ";}?>
Id Nikename Email
". $ Array [# id #]."". $ Array [# nikename #]."". $ Array [# email #]."
DbClose ();?>

13. delete table data

1234 Delete ("user", "where nikename = # beyondweb #"); // replace # with single quotation marks $ db-> dbClose ();?>

The above is BeyondWeb.cn's connection to the database with PHP, to achieve the most basic addition, deletion, modification, and query of all content. I hope this will serve as a valuable reference for you and give you valuable suggestions, to improve the quality of the 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.