Home  >  Article  >  Backend Development  >  PHP and PDO: How to query and display data from a database

PHP and PDO: How to query and display data from a database

PHPz
PHPzOriginal
2023-07-28 15:33:201477browse

PHP and PDO: How to query and display data in the database

Introduction:
In Web development, the database is a very important component. PHP is a powerful programming language, and PDO (PHP Data Objects) is an extension in PHP for accessing databases. This article will introduce you how to use PHP and PDO to query and display data in the database.

1. Connect to the database
First, we need to connect to the database through PDO. The following is a sample code to connect to a MySQL database:

$db_host = 'localhost';
$db_name = 'test';
$db_user = 'root';
$db_pass = '';

try {
  $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "数据库连接成功!";
} catch(PDOException $e) {
  echo "连接失败: " . $e->getMessage();
}

In the above code, we use the PDO constructor to create a database connection and set some connection options. If the connection is successful, "Database connection successful!" will be output, otherwise an error message will be output.

2. Query data
Once we successfully connect to the database, we can execute the query statement to obtain the data. The following is a simple query example:

$sql = "SELECT * FROM users";
$stmt = $conn->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($results as $row) {
  echo $row['name'] . ', ' . $row['email'] . '<br>';
}

The above code executes a simple SELECT statement to obtain all data in the data table named "users". We used the prepare() method to prepare the SQL statement, the execute() method to execute the SQL statement, and then the fetchAll() method to save the query results into an associative array. Finally, we use a foreach loop to traverse the array and display the "name" and "email" fields of each row of data.

3. Display data
After obtaining the data, we can display them on the Web page. The following is a simple example:

<!DOCTYPE html>
<html>
<head>
  <title>用户列表</title>
</head>
<body>
  <h1>用户列表</h1>

  <table>
    <tr>
      <th>姓名</th>
      <th>邮箱</th>
    </tr>

    <?php foreach ($results as $row): ?>
      <tr>
        <td><?php echo htmlspecialchars($row['name']); ?></td>
        <td><?php echo htmlspecialchars($row['email']); ?></td>
      </tr>
    <?php endforeach; ?>
  </table>
</body>
</html>

The above code uses the basic structure of HTML to create a simple user list page. We used a foreach loop to traverse the data array, and used the htmlspecialchars() function to escape the data to prevent XSS attacks.

Summary:
This article introduces how to use PHP and PDO to query and display data in the database. We first connect to the database, then execute the query statement to obtain the data, and finally display the data on the Web page. By using the power of PDO, we can easily interact with the database and flexibly handle the display and processing of data. I hope this article will help you use PHP and PDO to operate databases in web development.

The above is the detailed content of PHP and PDO: How to query and display data from a database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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