Home  >  Article  >  Backend Development  >  How to query the database in php and output the results

How to query the database in php and output the results

PHPz
PHPzOriginal
2023-04-11 15:05:552125browse

PHP is a programming language widely used for web development. In web applications, querying the database is a very common operation. In this article, we will discuss how to query a database using PHP and output the results.

First, we need to ensure that our PHP environment has the extensions and drivers for operating the database installed and configured. Typically, PHP comes with a MySQL driver. If you are using another database, such as Oracle or SQL Server, you need to install the corresponding extensions and drivers.

Next, we need to connect to the database. In PHP, use mysqli or PDO extensions to implement database connections. The mysqli extension is an encapsulation of the MySQL database, while the PDO extension is a unified encapsulation of multiple databases. In this article, we will use the mysqli extension for an example demonstration.

The following is a basic PHP script for connecting to a MySQL database and querying data:

<?php
$host = "localhost"; // 数据库主机名
$user = "username"; // 数据库用户名
$password = "password"; // 数据库密码
$dbname = "database_name"; // 数据库名

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

// 检查连接是否成功
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
} 

// 查询数据
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

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

$conn->close();
?>

In the above script, we use the mysqli_connect function to create a database connection. If the connection fails, an error message is printed and the script is terminated.

Query data using SQL statements. Here we only query all the data in the data table. Use the mysqli_query function to execute the SQL statement and store the result in the $result variable.

Finally, we use the mysqli_fetch_assoc function to traverse a row of data in the result set and output the data to the screen. If the result set is empty, "0 results" are output.

In addition to querying all data, we can also use the WHERE clause to query data under specific conditions:

$sql = "SELECT * FROM table_name WHERE age > 18";
$result = $conn->query($sql);

The above statement will query all data in the table whose age is greater than 18.

In addition to outputting data to the screen, we can also store the data in variables for subsequent processing:

$data = array();
if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    $data[] = $row;
  }
} else {
  echo "0 结果";
}

The above code stores the result set in the $data array.

Summary:

To query the database and output data in PHP, you can use the following steps:

  1. Connect to the database using mysqli or PDO extension.
  2. Use SQL statements to query data.
  3. Use the fetch function to traverse the result set.
  4. Output the results or store them in variables for processing.

I hope this article will be helpful to PHP query database and result output.

The above is the detailed content of How to query the database in php and output the results. 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