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
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
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) {?>
Id |
Nikename |
Email |
MyArray ($ select) {echo"
"; Echo"
". $ Array [# id #]." | "; // Replace # with the single quotation mark echo"
". $ Array [# nikename #]." | "; // Replace # with the single quotation mark echo"
". $ Array [# email #]." | "; // Replace # with the single quotation mark echo"
";}?>
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.