Home >Backend Development >PHP Problem >How to query statements in php

How to query statements in php

PHPz
PHPzOriginal
2023-04-05 14:37:12545browse

PHP is a popular web programming language that can be used with various types of databases. In PHP, querying the database is very important because we need to retrieve data from the database rather than simply display the data.

There are two forms of query statements in PHP: MySQLi and PDO. MySQLi is a MySQL extension, and PDO is a database abstraction layer.

MySQLi Query Statement

Performing a MySQLi query in PHP requires the following steps:

  1. Connect to the MySQL server

In PHP Connecting to the MySQL server can be achieved through the mysqli_connect function. The function needs to pass four parameters - host name, user name, password and database. Here is the sample code to connect to the MySQL server:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
  1. Prepare and execute the query

Once connected to the MySQL server, we can prepare and execute the query. The following is a sample code for querying the database using MySQLi:

$sql = "SELECT * FROM users WHERE age > 18";
$result = mysqli_query($con, $sql);

The above code will select all users older than 18 years old from the data table named "users". The result will be stored in the $result variable.

  1. Processing the results

Once we have the results, we need to extract the data from the result set and process it. The following is a sample code to extract data from the result set:

while($row = mysqli_fetch_array($result)) {
    echo $row['name'] . " " . $row['age'];
}

The above code will extract each row of data from the result set and print each row of data using the values ​​of the "name" and "age" columns.

PDO query statement

PDO is a database abstraction layer that supports multiple types of databases and multiple types of queries. The following are the steps to use PDO to query the database in PHP:

  1. Connect to the database

To connect to the database in PHP, you can use the constructor of the PDO object. For MySQL, the code is as follows:

$dbh = new PDO('mysql:host=localhost;dbname=my_db', 'my_user', 'my_password');
  1. Prepare and execute the query

Once connected to the database, we can prepare and execute the query. The following is a sample code for querying the database using PDO:

$sql = "SELECT * FROM users WHERE age > 18";
$sth = $dbh->prepare($sql);
$sth->execute();

The above code will select all users older than 18 years old from the data table named "users". The result will be stored in the $sth variable.

  1. Processing the results

Once we have the results, we need to extract the data from the result set and process it. The following is a sample code to extract data from the result set:

while($row = $sth->fetch()) {
    echo $row['name'] . " " . $row['age'];
}

The above code will extract each row of data from the result set and print each row of data using the values ​​of the "name" and "age" columns.

Summary

In PHP, querying the database is a basic task, which can be easily completed through MySQLi and PDO query statements. No matter which method you choose, you need to connect to the database, prepare and execute the query, and then process the results. I hope this article can help you better understand query statements in PHP.

The above is the detailed content of How to query statements in php. 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:How to set php cache timeNext article:How to set php cache time