Home > Backend Development > PHP Problem > How to determine whether the query result is empty in php

How to determine whether the query result is empty in php

PHPz
Release: 2023-04-23 18:05:14
Original
1460 people have browsed it

In PHP, we often need to query the database and obtain query results. During this process, sometimes the query results may be empty (that is, there are no records that meet the query conditions). At this time, we need to judge the query results for subsequent processing.

So how to determine whether the query result is empty in PHP? Let’s explain it in detail below.

  1. Using the mysqli_num_rows function

The mysqli_num_rows function is a function in PHP used to get the number of rows in the query result. When the query result is empty, the function returns 0. Therefore, we can use the mysqli_num_rows function to determine whether the query result is empty.

Sample code:

// 连接数据库
$conn = mysqli_connect('localhost', 'root', '123456', 'test');

// 查询结果
$result = mysqli_query($conn, "SELECT * FROM users WHERE name = '张三'");

// 判断查询结果是否为空
if(mysqli_num_rows($result) == 0) {
    echo '查询结果为空';
} else {
    // 处理查询结果
    while($row = mysqli_fetch_assoc($result)) {
        // ...
    }
}

// 关闭数据库连接
mysqli_close($conn);
Copy after login

In the above code, we first connect to the database, and then execute a query statement to query the user named "Zhang San". Next, we use the mysqli_num_rows function to get the number of rows in the query result and determine whether it is 0. If it is 0, it means that the query result is empty; otherwise, we can use the while loop to traverse the query results and perform subsequent processing.

  1. Use empty function

The empty function is a commonly used function in PHP, which is used to determine whether a variable is empty. When the query result is empty, we can use the empty function to judge.

Sample code:

// 连接数据库
$conn = mysqli_connect('localhost', 'root', '123456', 'test');

// 查询结果
$result = mysqli_query($conn, "SELECT * FROM users WHERE name = '张三'");

// 判断查询结果是否为空
if(empty(mysqli_fetch_assoc($result))) {
    echo '查询结果为空';
} else {
    // 处理查询结果
    mysqli_data_seek($result, 0); // 将结果指针重置到第一条记录
    while($row = mysqli_fetch_assoc($result)) {
        // ...
    }
}

// 关闭数据库连接
mysqli_close($conn);
Copy after login

In the above code, we first connect to the database, and then execute a query statement to query the user named "Zhang San". Next, we use the mysqli_fetch_assoc function to obtain the first record of the query result, and pass it as a parameter to the empty function for judgment. If the return value is true, it means that the query result is empty; otherwise, we can use the while loop to traverse the query results and perform subsequent processing.

  1. Use the is_null function

The is_null function is a function in PHP that is used to determine whether a value is null. When the query result is empty, we can also use the is_null function to judge.

Sample code:

// 连接数据库
$conn = mysqli_connect('localhost', 'root', '123456', 'test');

// 查询结果
$result = mysqli_query($conn, "SELECT * FROM users WHERE name = '张三'");

// 判断查询结果是否为空
if(is_null(mysqli_fetch_assoc($result))) {
    echo '查询结果为空';
} else {
    // 处理查询结果
    mysqli_data_seek($result, 0); // 将结果指针重置到第一条记录
    while($row = mysqli_fetch_assoc($result)) {
        // ...
    }
}

// 关闭数据库连接
mysqli_close($conn);
Copy after login

In the above code, we first connect to the database, and then execute a query statement to query the user named "Zhang San". Next, we use the mysqli_fetch_assoc function to obtain the first record of the query result, and pass it as a parameter to the is_null function for judgment. If the return value is true, it means that the query result is empty; otherwise, we can use the while loop to traverse the query results and perform subsequent processing.

Summary

In PHP, we can use the mysqli_num_rows function, empty function or is_null function to determine whether the query result is empty. Each of these methods has its own advantages and disadvantages, and you need to choose the method that suits you according to the specific business scenario.

No matter which method is used, we should pay attention to the opening and closing of the database connection to avoid problems such as resource leakage. In addition, when the query result is empty, we can also prompt the user to perform relevant operations to improve the user experience.

The above is the detailed content of How to determine whether the query result is empty 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