What types of MySQL query statements are there in PHP?

WBOY
Release: 2024-06-03 17:30:02
Original
707 people have browsed it

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.

PHP 中的 MySQL 查询语句有哪些类型?

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");
Copy after login

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");
Copy after login

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)");
Copy after login

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");
Copy after login

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");
Copy after login

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();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!