Home>Article>Backend Development> How to call PHP methods to operate the database
If you want to learn PHP, then using a database is an insurmountable hurdle. Because working with data is an essential step in any website development. In this article, we will learn how to call PHP methods to operate the database.
PHP connects to the database
Before using the database, we first need to ensure that the database has been correctly connected. In PHP, we usually use the mysqli() function to connect to the database. This function receives four parameters, including host address, user name, password and database name. An example is as follows:
$conn = mysqli('localhost', 'root', 'password', 'testdb');
Among them, localhost represents the host address, root represents the user name, password represents the password, and testdb represents the connected database name.
If the database is successfully connected, we can use the mysqli_select_db() function to select the database to use. An example is as follows:
mysqli_select_db($conn, 'testdb');
After connecting to the database, you can then perform database operations.
PHP executes SQL statements
PHP provides many methods for processing SQL statements. In this article we will introduce the most common method-mysqli_query() function. We can execute the SQL statement by passing it as a parameter to the mysqli_query() function and store the result in the $result variable.
For example, the following is a SQL query statement:
SELECT * FROM users WHERE age > 18;
can be executed through the mysqli_query() function, and the query results are stored in the $result variable:
$result = mysqli_query($conn, "SELECT * FROM users WHERE age > 18");
After getting After querying the results, we can use the mysqli_fetch_array() function to process the query results. For example:
while($row = mysqli_fetch_array($result)) { echo $row['name'] . ' ' . $row['age']; }
mysqli_fetch_array() function will read the query results row by row, and then return the data record of the current row. We can use the array method to access the $row variable and read the data in it.
PHP Insert Data
To insert data into the database, we need to use the INSERT statement. The following is a sample code:
$query = "INSERT INTO users (name, age, address) VALUES ('Tom', 20, 'New York')"; $result = mysqli_query($conn, $query); if($result) { echo "数据插入成功!"; } else { echo "数据插入失败!"; }
This insert statement will insert a user named Tom, age 20, and address New York into the data table named users. If the insertion is successful, "Data insertion successful!" will be output, otherwise "Data insertion failed!" will be output.
PHP update data
If you want to update the data in the database, we need to use the UPDATE statement. The following is a sample code:
$query = "UPDATE users SET age = 25 WHERE name = 'Tom'"; $result = mysqli_query($conn, $query); if($result) { echo "数据更新成功!"; } else { echo "数据更新失败!"; }
This update statement will update the age of the user named Tom from 20 to 25. If the update is successful, "Data update successful!" will be output, otherwise "Data update failed!" will be output.
PHP Delete Data
If you want to delete data in the database, we need to use the DELETE statement. The following is a sample code:
$query = "DELETE FROM users WHERE name = 'Tom'"; $result = mysqli_query($conn, $query); if($result) { echo "数据删除成功!"; } else { echo "数据删除失败!"; }
This delete statement will delete the user named Tom from the data table named users. If the deletion is successful, "Data deletion successful!" will be output, otherwise "Data deletion failed!" will be output.
Summary
In PHP, we can use the mysqli() function to connect to the database. After the connection is successful, we can use mysqli_query(), mysqli_fetch_array() and other functions to execute SQL statements and obtain query results. In order to update the database and delete data, we need to use UPDATE and DELETE statements. The above method is only the basic operation of PHP using the database. If we want to learn more advanced database operations, we need to continue to learn and explore.
The above is the detailed content of How to call PHP methods to operate the database. For more information, please follow other related articles on the PHP Chinese website!