Home>Article>Backend Development> How to query statements in php
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:
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");
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.
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:
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');
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.
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!