Home > Backend Development > PHP Problem > How to determine if sql is not empty in php

How to determine if sql is not empty in php

藏色散人
Release: 2023-03-05 16:54:01
Original
2514 people have browsed it

php method to determine that sql is not empty: first find out the information through the sql query function code; then obtain the number of rows in the select result set through "mysql_query" in the php file; finally determine the sql query results through the if statement That’s it.

How to determine if sql is not empty in php

Recommended: "PHP Video Tutorial"

PHP and mysql, the golden partners, work well together, but Occasionally, I will encounter some small needs and I don’t know what to do. For example, what I’m going to talk about today: How to determine whether the result set of a SQL statement query is empty!

Let’s take querying student information as an example to see how to achieve our needs.

First of all, let’s take a look at what the data stored in our data table “student” looks like; 3

2 Li Sinan 15 18 2

3 Wang Meili 16 17 5

Let’s take a look at the sql query function code. We want to add the student information of 16 years old Find them all;

<?php$sql = "select * from `student` where `age`=&#39;16&#39;;";$rows = mysql_query($rs);?>
<ul>
    <?php        while($rows=mysql_fetch_array($rs)){    ?>
        <li>姓名:<?php echo $rows[&#39;stuname&#39;];?></li>
        <li>性别:<?php echo $rows[&#39;gender&#39;];?></li>
        <li>年龄:<?php echo $rows[&#39;age&#39;];?></li>
        <li>年级:<?php echo $rows[&#39;grade&#39;];?></li>
        <li>班级:<?php echo $rows[&#39;class&#39;];?></li>
    <?php
        }    ?>
</ul>
Copy after login

The above is the query function. When the result set is not empty, everything is normal. When the data set is empty, you will get a

blank ul tag

, As a user, I don’t knowwhy I didn’t get the data. At this time, we need to

give the user a prompt message, then we need to determine whether this result set is empty! How to determine whether the result set is empty? There are two methods:

<?php
//方法一 获取select结果集的行数
$rows=mysql_query("select * from `student` where `age`=&#39;16&#39;;");
if (mysql_num_rows($rows) < 1){
echo &#39;查询无数据!&#39;;
}

//方法二 返回上一次操作受影响的行数
$rows=mysql_query("select * from `student` where `age`=&#39;16&#39;;");
if(!mysql_affected_rows()){
    echo &#39;查询无数据!&#39;;
}
?>知道了方法,那么把方法套到我们的代码中看看效果吧
Copy after login
//方法一
<?php
$sql = "select * from `student` where `age`=&#39;16&#39;;";
$rows = mysql_query($rs);
?>
<ul>
    <?php
    if (mysql_num_rows($rs) < 1){
        echo &#39;查询无数据!&#39;;
    }else{
        while($rows=mysql_fetch_array($rs)){
    ?>
        <li>姓名:<?php echo $rows[&#39;stuname&#39;];?></li>
        <li>性别:<?php echo $rows[&#39;gender&#39;];?></li>
        <li>年龄:<?php echo $rows[&#39;age&#39;];?></li>
        <li>年级:<?php echo $rows[&#39;grade&#39;];?></li>
        <li>班级:<?php echo $rows[&#39;class&#39;];?></li>
    <?php
        }
    }
    ?>
</ul>

//方法二
<?php
$sql = "select * from `student` where `age`=&#39;16&#39;;";
$rows = mysql_query($rs);
?>
<ul>
    <?php
    if(mysql_affected_rows()){
        while ($rows=mysql_fetch_assoc($rs)){
    ?>
        <li>姓名:<?php echo $rows[&#39;stuname&#39;];?></li>
        <li>性别:<?php echo $rows[&#39;gender&#39;];?></li>
        <li>年龄:<?php echo $rows[&#39;age&#39;];?></li>
        <li>年级:<?php echo $rows[&#39;grade&#39;];?></li>
        <li>班级:<?php echo $rows[&#39;class&#39;];?></li>
    <?php
        }
    }else {
        echo "查无数据!";
    }
    ?>
</ul>
Copy after login
OK, you’re done. Now you will get a prompt when no data can be found!

The above is the detailed content of How to determine if sql is not empty in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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