Home > Article > Backend Development > How to query the database and intercept field values in php
PHP is a widely used server-side scripting language that is widely used in web development. In PHP, querying the database is a common task, and in this process, intercepting field values is also a common operation. This article will introduce how to use PHP to query the database and intercept field values.
1. Connect to the database
In PHP, you first need to connect to the database to operate. Common databases include MySQL, Oracle, SQL Server, etc. Taking MySQL as an example, the following is a code example for connecting to a MySQL database:
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database_name"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
In the above code, $servername is the MySQL server address, $username is the MySQL username, $password is the MySQL password, and $dbname is the database that needs to be connected. The MySQL database name. The mysqli_connect() function is used to connect to the MySQL database.
2. Query the database
After the connection is successful, you can query the database. The following is a simple query example:
$sql = "SELECT id, name, age FROM users"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // 输出每行数据 while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>"; } } else { echo "0 结果"; }
In the above code, $sql is the SQL statement that needs to be executed. In this example, the id, name, and age fields in the users table are queried. The mysqli_query() function executes a SQL statement and returns a result set. The mysqli_num_rows() function is used to obtain the number of rows in the query result set. The mysqli_fetch_assoc() function is used to return an associative array. The key of the array is the field name and the value is the field value.
3. Intercepting field values
Sometimes, we only need part of the field value, and then we need to intercept the field value. For example, if we only need to get the first 10 characters of a field value, we can use the substr() function to intercept it. The following is an example:
$sql = "SELECT name, description FROM products"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // 输出每行数据 while($row = mysqli_fetch_assoc($result)) { echo "Name: " . $row["name"]. " - Description (10 chars): " . substr($row["description"], 0, 10). "<br>"; } } else { echo "0 结果"; }
In the above code, $sql is the SQL statement that needs to be executed. In this example, the name and description fields in the products table are queried. The substr() function is used to intercept a string. The first parameter is the string to be intercepted, the second parameter is the starting position (starting from 0), and the third parameter is the intercepted length.
4. Summary
The above is how to use PHP to query the database and intercept field values. In practical applications, adjustments need to be made according to specific circumstances. For example, when you need to intercept multiple fields, you can use the substr() function multiple times for processing. At the same time, you also need to pay attention to security issues such as SQL injection, and try to avoid using user-entered data to construct SQL statements.
The above is the detailed content of How to query the database and intercept field values in php. For more information, please follow other related articles on the PHP Chinese website!