MySQL query statement types in PHP include: SELECT: Retrieve data from the table. WHERE: Filter SELECT results based on conditions. INSERT: Insert new records into the table. UPDATE: Update an existing record. DELETE: Delete records from the table.
MySQL query statement type in PHP
MySQL query statement is a command that interacts with the MySQL database and queries data. PHP provides a variety of functions to execute MySQL queries, and each function corresponds to different types of query statements.
SELECT statement
The SELECT statement is used to retrieve data from a database table. The syntax is as follows:
$result = $conn->query("SELECT * FROM table_name");
WHERE clause
The WHERE clause is used to filter results based on specific criteria. The syntax is as follows:
$result = $conn->query("SELECT * FROM table_name WHERE condition");
INSERT statement
The INSERT statement is used to insert new records into a database table. The syntax is as follows:
$conn->query("INSERT INTO table_name (column1, column2) VALUES (value1, value2)");
UPDATE statement
The UPDATE statement is used to update existing records in a database table. The syntax is as follows:
$conn->query("UPDATE table_name SET column1 = value1 WHERE condition");
DELETE statement
The DELETE statement is used to delete records from a database table. The syntax is as follows:
$conn->query("DELETE FROM table_name WHERE condition");
Practical case
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询所有记录 $result = $conn->query("SELECT * FROM users"); // 输出结果 if ($result->num_rows > 0) { // 输出每行数据 while($row = $result->fetch_assoc()) { echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>"; } } else { echo "0 results"; } // 关闭连接 $conn->close(); ?>
The above is the detailed content of What types of MySQL query statements are there in PHP?. For more information, please follow other related articles on the PHP Chinese website!