How to select data from MySQL table using PHP?

WBOY
Release: 2024-06-01 13:20:56
Original
691 people have browsed it

How to use PHP to select data from a MySQL table to establish a database connection, using MySQLi or PDO extensions. Prepare query statements. Execute the query and get the results. Use a loop to iterate through the result data and output it.

如何使用 PHP 从 MySQL 表中选择数据?

How to select data from a MySQL table using PHP

Preface

In In PHP, we can select data from MySQL database using MySQLi or PDO extension. This article demonstrates how to achieve this using both extensions.

Using MySQLi extension

1. Establish database connection

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}
Copy after login

2. Prepare query

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // 输出数据
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
  }
} else {
  echo "0 结果";
}

$conn->close();
Copy after login

Use PDO extension

1. Establish database connection

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  die("连接失败: " . $e->getMessage());
}
Copy after login

2. Prepare query

$sql = "SELECT * FROM users";
$stmt = $conn->prepare($sql);
$stmt->execute();

$result = $stmt->fetchAll();

foreach($result as $row) {
  echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}

$conn = null;
Copy after login

Practical case

Suppose we have a MySQL table named "users", which stores user data. To select all data from this table, we can use приведенный above code as shown below:

// 使用 MySQLi 扩展
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // 输出数据
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
  }
} else {
  echo "0 结果";
}

$conn->close();
Copy after login

Similarly, we can use PDO extension to select data as shown below:

// 使用 PDO 扩展
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  die("连接失败: " . $e->getMessage());
}

$sql = "SELECT * FROM users";
$stmt = $conn->prepare($sql);
$stmt->execute();

$result = $stmt->fetchAll();

foreach($result as $row) {
  echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}

$conn = null;
Copy after login

The above is the detailed content of How to select data from MySQL table using 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!