With the continuous development of Internet technology, website development has become an indispensable part of people's production and life. As a powerful web development language, PHP is widely used in the field of Internet development. In website development, the database is an indispensable part, and the query operation of the database is very important. This article will focus on how PHP queries a column of data in the database.
1. Preparation
The interaction between PHP language and database usually requires the help of database extension. This article will take MySQL database as an example. Before using PHP to query the MySQL database, we need to make the following preparations:
;extension=php_mysql.dll
If there is a semicolon in front of this line, it means that the MySQL extension is not enabled. You need to remove the semicolon and restart the web server. Take effect.
Prepare these pieces of information and save them into a PHP file.
Before connecting to the MySQL database, we need to use the mysqli_connect() function provided by PHP to establish a connection. The parameters of this function include:
We can use the following code to connect to the database:
$host = "localhost"; $username = "root"; $password = "123456"; $dbname = "test"; $conn = mysqli_connect($host, $username, $password, $dbname);
This code will establish a database connection named test and store the connection information in the variable $conn.
2. Query a column of data
Querying a column of database is a common requirement, usually to count the number of a certain indicator or to display related data on the front end. Before querying a column in the database, we need to determine the column name and query statement to be queried. For example, we need to count the proportion of boys and girls in the student list. We can use the following code to query the number of boys and girls:
$sql = "SELECT count(*) FROM student WHERE gender='男'"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_array($result); $male_count = $row[0]; $sql = "SELECT count(*) FROM student WHERE gender='女'"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_array($result); $female_count = $row[0];
In this code, we use the SELECT statement to query the number of students who meet the conditions, and The result is stored in the $row variable. Note that before operating on the query results, we need to call the mysqli_fetch_array() function to convert the query results into an array form in PHP. At this point, we have successfully queried the number of boys and girls and stored the results in $male_count and $female_count.
3. Summary
This article introduces how to use PHP to query a column of data, including connecting to the MySQL database and query methods. When we need to query the database during website development, the above steps are very critical. In actual development, we also need to pay attention to security issues such as SQL injection to ensure the security and stability of the website.
The above is the detailed content of Detailed explanation of using php to query a column of data in the database. For more information, please follow other related articles on the PHP Chinese website!