As the needs of website development become more and more complex, data storage becomes more and more important. A database is a warehouse that stores and processes data, making it easy for us to store and query data in an efficient manner. PHP and MySQL are two open source software that can be combined to easily develop web-based applications. This article will introduce in detail how to use PHP to connect to the MySQL database and implement operations of adding, modifying and deleting data.
Connecting to MySQL database
In PHP, connecting to a MySQL database requires the use of the MySQLi (MySQL improved) extension. This extension provides all the functions and methods needed to work with MySQL database.
To connect to the MySQL database, you need to establish a database connection first. To establish a MySQLi connection, you need to use the mysqli_connect function as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
In the above example, we use the mysqli_connect function to create a MySQLi connection and use $servername, $username, $password and $ The dbname variable represents the required connection parameters. After the connection is successful, we print a message to confirm that the connection has been established.
Insert data
To insert data into the MySQL database, you need to use the mysqli_query function to send SQL statements to the connected database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
In this example, we inserted a row of data into the MyGuests table, including first name, last name, and email address. If inserting data is successful, we will output a message.
Update data
To update data in the database, you can use the mysqli_query function and the UPDATE statement. Here is an example that updates the email addresses of everyone with the last name Doe in the MyGuests table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
After updating the email addresses of everyone with the last name Doe in the MyGuests table, we will output the number of rows affected.
Delete data
To delete data from the MySQL database, you need to use the mysqli_query function and the DELETE statement. The following is an example of removing everyone with the last name Doe from the MyGuests table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
After deleting the records for everyone with the last name Doe in the MyGuests table, we will output the number of rows affected.
Summary
In this article, we introduced how to use PHP and MySQLi extensions to connect to a MySQL database and implement insert, update, and delete data operations. Of course, here are only the most basic operations and syntax, and there are more examples and methods that we need to learn and understand in depth. In actual use, we also need to pay attention to some security issues to avoid security issues such as SQL injection. I hope this article can help everyone quickly get started using PHP and MySQL to develop powerful web applications.
The above is the detailed content of How to implement addition, modification and deletion of php mysql. For more information, please follow other related articles on the PHP Chinese website!