Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the most basic addition, deletion, modification and query of PHP connection database

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

PHP connection database how to achieve the most basic addition, deletion, modification and query, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

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

PHP connects to the database to achieve the most basic addition, deletion, modification and query (process-oriented) this article has introduced the method of PHP connecting to the database and the most basic operation on the database, but it is not modularized. All the code is concentrated in the presentation page, resulting in code redundancy, which is not conducive to maintenance and code reuse. Then in this article, we will use object-oriented knowledge to encapsulate the connection and basic operation of the database, which greatly avoids the duplication of code.

Let's create a database operation class:

1. Create the mysql_class.php file and then create the Mysql class in the file and define the variables

1 2 3 4 5 6 7 8 9 10 11

2. Initialize the class by constructor

1 2 3 4 5 6 7function

_ _ construct ($host,$root,$password,$database) {$this- > host

= $host; $this- > root

= $root; $this- > password

= $password; $this- > database

= $database; $this- > connect ();}

For the connect () method, let's talk about it next.

3. The method of creating a connection database and closing the database

1 2 3 4 5 6 7 8 9function

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 (), mysql_num_rows () functions

1 2 3 4 5 6 7 8 9 10 11function

Query ($sql) {return

Mysql_query ($sql);} function

MyArray ($result) {return

Mysql_fetch_array ($result);} function

Rows ($result) {return

Mysql_num_rows ($result);}

5. Custom query data method

1 2 3function

Select ($tableName,$condition) {return

$this- > query ("SELECT"

* FROM $tableName $condition ");}

6. Custom data insertion method

1 2 3function

Insert ($tableName,$fields,$value) {$this- > query ("INSERT

INTO $tableName $fields VALUES$value ");}

7. Customize the method of modifying data

1 2 3function

Update ($tableName,$change,$condition) {$this- > query ("UPDATE

$tableName SET $change $condition ");}

8. Custom method for deleting data

1 2 3function

Delete ($tableName,$condition) {$this- > query ("DELETE

FROM $tableName $condition ");}

Now that the database operation class is packaged, let's take a look at how to use it.

We still use PHP to connect to the database and implement the most basic addition, deletion, modification and query (process-oriented) of the database and tables (data in the table is added by itself):

9. Let's instantiate the database operation class first

1$ db

= new Mysql ("localhost", "root", "admin", "beyondweb_test")

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

Then we create a test.php file, first introducing the mysql_class.php file into

1 2 3

And then let's get started.

10. Insert data into the table

1 2 3 4

11. Modify data in the table

1 2 3 4

12. Query the data in the table and output

12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 id nikename email

13. Delete data from the table

1234 after reading the above contents, have you mastered how to connect to the PHP database to achieve the most basic methods of adding, deleting, modifying and searching? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report