Home > PHP Framework > ThinkPHP > body text

How does thinkphp determine whether the query results contain data?

PHPz
Release: 2023-04-11 15:28:16
Original
1258 people have browsed it

When using the ThinkPHP framework for data operations, we often need to query the data in the database. Whether querying using ORM or handwriting SQL statements, it is essential knowledge for developers to determine whether the query results exist and how to process the query results. This article mainly describes how to use the ThinkPHP framework to judge query results.

1. Query using ORM

The ThinkPHP framework used for ORM operations provides a wealth of methods for query operations. Among these operations, the most commonly used methods are the select method and the find method. Among them, the select method returns a result set, and the find method returns a result record. When the query has no results, the results returned by both are empty arrays or null values. Below we use sample code to demonstrate how to make a judgment.

  1. select method query
// 查询student表中所有年龄小于18岁的学生信息
$map['age'] = ['lt', 18];
$res = Db::name('student')->where($map)->select();

if($res){ // 判断结果集是否存在
    foreach($res as $row){
        // 存在结果,对查询结果进行处理
        echo $row['name'].'的年龄是'.$row['age'].'岁'.'<br>';
    }
}else{
    // 无结果,给出提示信息
    echo '很遗憾,没有找到符合条件的学生列表';
}
Copy after login
  1. find method query
// 查询student表中年龄为21岁的学生信息
$map['age'] = 21;
$res = Db::name('student')->where($map)->find();

if($res){ // 判断结果记录是否存在
    // 存在结果,对查询结果进行处理
    echo $res['name'].'的年龄是'.$res['age'].'岁'.'<br>';
}else{
    // 无结果,给出提示信息
    echo '很遗憾,没有找到符合条件的学生记录';
}
Copy after login

2. Use SQL query

For For developers who are familiar with SQL statement operations, handwritten query statements are definitely a good choice. When performing handwritten queries, we can obtain the result set and result record information through the fetch method and fetchColumn method respectively. Below we use sample code to demonstrate how to make a judgment.

  1. fetch method query
// 查询student表中所有性别为男性的学生信息
$sql = "SELECT * FROM student WHERE gender='male'";
$res = Db::query($sql);

if($res){ // 判断结果集是否存在
    foreach($res as $row){
        // 存在结果,对查询结果进行处理
        echo $row['name'].'的性别是'.$row['gender'].'<br>';
    }
}else{
    // 无结果,给出提示信息
    echo '很遗憾,没有找到符合条件的学生列表';
}
Copy after login
  1. fetchColumn method query
// 查询student表中姓名为张三的学生ID
$sql = "SELECT id FROM student WHERE name='张三'";
$res = Db::query($sql);

if($res){ // 判断结果集是否存在
    // 存在结果,对查询结果进行处理
    echo '张三的学生ID是:'.$res[0]['id'];
}else{
    // 无结果,给出提示信息
    echo '很遗憾,没有找到符合条件的学生记录';
}
Copy after login

3. Summary

By ORM and the use of SQL query methods are explained. We can see that in these methods, the judgment, processing and output of query results are a very important link. Only by judging the query results can we perform subsequent operations based on the query results. Therefore, it is recommended that developers pay attention to the judgment and processing of result sets and result records when performing data operations.

The above is the detailed content of How does thinkphp determine whether the query results contain data?. 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