Home  >  Article  >  Backend Development  >  php database query display

php database query display

WBOY
WBOYOriginal
2023-05-06 19:37:071274browse

As a programming language widely used in Web development, PHP basically cannot avoid database query and display operations. This article will briefly introduce how to use PHP to query the database and present the query results to the user in a beautiful way.

1. Connect to the database
Before using PHP to query the database, you need to connect to the database first. The following is a code example for connecting to a MySQL database:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "databaseName";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

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

2. Database query
PHP provides a variety of methods for database query. Here are two commonly used methods: mysqli and PDO.

  1. mysqli
    Mysqli is a MySQL database operation extension provided by PHP. It provides a set of object-oriented APIs and supports preventing SQL injection attacks. The following is a code example for querying the database:
<?php
$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>";
    }
} else {
    echo "0 结果";
}
$conn->close();
?>
  1. PDO
    PDO is a general database operation extension provided by PHP. It supports multiple database types and only needs to change the connection string. Change database. The code example of using PDO to query is as follows:
<?php
$sql = "SELECT id, name, age FROM users";
$stmt = $pdo->prepare($sql);
$stmt->execute();

if ($stmt->rowCount() > 0) {
    // 输出每行数据
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>";
    }
} else {
    echo "0 结果";
}
?>

3. Data display
After querying the data, we need to display the data to the user. Here we use HTML and CSS to beautify the display effect.

  1. List display
    The following is a simple data list display:
<ul>
<?php
while($row = $result->fetch_assoc()) {
    echo "<li>". $row["name"]. " - ". $row["age"]. "</li>";
}
?>
</ul>
  1. Table display
    The following is a simple but easy to understand style Table display:
<table>
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <?php while($row = $result->fetch_assoc()): ?>
  <tr>
    <td><?php echo $row['id']; ?></td>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['age']; ?></td>
  </tr>
  <?php endwhile; ?>
</table>

4. Security Precautions
When performing database query and display operations, you need to pay attention to several security issues:

  1. SQL injection Attack: Using prepared statements can effectively avoid SQL injection attacks.
  2. Data formatting: Ensure that the displayed data meets user expectations and does not cause security issues.
  3. Authorization and access control: MySQL provides a very fine-grained authorization and access control mechanism, which can authorize and deauthorize users and databases through GRANT and REVOKE statements.

Summary
This article briefly introduces the operation of database query and display in PHP, and provides sample code and further security considerations. I hope readers can develop safer, more beautiful, and more practical Web applications based on this knowledge.

The above is the detailed content of php database query display. 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
Previous article:php unified error codeNext article:php unified error code