Home > Backend Development > PHP Problem > How to query name, gender and class in php

How to query name, gender and class in php

PHPz
Release: 2023-03-31 10:03:34
Original
1352 people have browsed it

PHP implements querying name, gender, class

In practical applications, many websites need to implement the function of querying user information, such as querying name, gender, class and other information. PHP, as a popular server-side scripting language, can be used to achieve this function. This article will introduce how to query name, gender, class and other information through PHP code.

1. Create a database

Before using PHP to query user information, you need to create a database and a table to store user information. You can use MySQL or other types of databases. This article uses MySQL as an example. You can create a database named "user_info" in the MySQL client software, and then create a table named "users" in the database. The structure of the table is as follows:

CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar (50) NOT NULL,
sex varchar(10) NOT NULL,
class varchar(50) NOT NULL,
PRIMARY KEY (id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In the above table, "id" is the self-increasing primary key, "name" is the user's name, and "sex" is the user's gender. "class" is the user class.

2. Connect to the database

In PHP, use the mysqli extension to connect to the database. You can use the following code to connect to the database named "user_info":

$host = 'localhost'; //MySQL server address
$user = 'root'; / /MySQL username
$pass = 'password'; //MySQL password
$db = 'user_info'; //Database name
$conn = new mysqli($host, $user, $pass, $db);
if(mysqli_connect_errno()){
die('Connection failed:'.mysqli_connect_error());
}
?>

In the above code , "$host" is the address of the MySQL server, "$user" and "$pass" are the MySQL user name and password respectively, and "$db" is the database name. Use the mysqli_connect function to create a mysqli object, and then use the mysqli_connect_errno function to determine whether the connection failed.

3. Query user information

After connecting to the database, you can use the mysqli_query function to query user information. You can use the following code to query all user information:

$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if($result->num_rows > 0){
//Output data
while($row = $result->fetch_assoc()){

echo "姓名:" .$row["name"]. " 性别:" .$row["sex"]. " 班级:" .$row["class"]. "<br>";
Copy after login
Copy after login

}
}else{
echo "No data";
}
$conn->close();
?>

In the above code, "$sql " is the SQL query statement, and "$result" is the query result set. Use the mysqli_query method to execute the SQL statement and assign the result set to the variable $result. If the number of rows in $result is greater than 0, use a while loop to iterate through each row in the result set and output the name, gender, and class of each user. If the result set is empty, "No data" is output.

4. Query the information of the specified user

If you need to query the information of the specified user, you can use the WHERE clause in the query statement. For example, you can use the following code to query user information named "Zhang San":

$name = "Zhang San";
$sql = "SELECT * FROM users WHERE name='$name'";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){

echo "姓名:" .$row["name"]. " 性别:" .$row["sex"]. " 班级:" .$row["class"]. "<br>";
Copy after login
Copy after login

}
}else{
echo "No data";
}
$conn->close();
?>

In the above code, use the variable "$name" to store the name that needs to be queried, and use the "$name" variable in the SQL statement to query.

5. Summary

This article introduces how to use PHP to implement the function of querying user information such as name, gender, and class. First, you need to create a database and table of user information, then use the mysqli extension to connect to the database, and use the mysqli_query method to execute SQL query statements. Through the introduction of this article, readers can easily implement the function of querying user information and modify the code according to actual needs.

The above is the detailed content of How to query name, gender and class in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template