Home>Article>Backend Development> How to specify conditions to query the database in php
How to query the database by specifying conditions in php: 1. Create a user table in the database; 2. Use mysqli_select_db to select the database to be operated; 3. Specify query conditions through where; 4. Use the mysqli_fetch_assoc function to convert the results Just convert the set data into an array.
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to specify conditions to query the database in php?
In the mysql database, create a user table and insert multiple pieces of data into the table for testing.
Create a test.php file, and within the file, use the header() method to set the encoding format of the page to utf-8.
In the test.php file, connect to the mysql database and use mysqli_select_db to select the database to operate.
In the test.php file, create a sql statement to obtain the data of the user data table, and specify the query conditions through where, for example, the data with id 3 , and then use the mysqli_query function to execute the sql statement.
In the test.php file, use the mysqli_fetch_assoc function to convert the result set data into an array form, and use print_r to output the data.
Open the test.php file in the browser to view the results.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to specify conditions to query the database in php. For more information, please follow other related articles on the PHP Chinese website!