Home > Article > Backend Development > How to determine if sql is not empty in php
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.
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`='16';";$rows = mysql_query($rs);?> <ul> <?php while($rows=mysql_fetch_array($rs)){ ?> <li>姓名:<?php echo $rows['stuname'];?></li> <li>性别:<?php echo $rows['gender'];?></li> <li>年龄:<?php echo $rows['age'];?></li> <li>年级:<?php echo $rows['grade'];?></li> <li>班级:<?php echo $rows['class'];?></li> <?php } ?> </ul>
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`='16';");
if (mysql_num_rows($rows) < 1){
echo '查询无数据!';
}
//方法二 返回上一次操作受影响的行数
$rows=mysql_query("select * from `student` where `age`='16';");
if(!mysql_affected_rows()){
echo '查询无数据!';
}
?>知道了方法,那么把方法套到我们的代码中看看效果吧
//方法一
<?php
$sql = "select * from `student` where `age`='16';";
$rows = mysql_query($rs);
?>
<ul>
<?php
if (mysql_num_rows($rs) < 1){
echo '查询无数据!';
}else{
while($rows=mysql_fetch_array($rs)){
?>
<li>姓名:<?php echo $rows['stuname'];?></li>
<li>性别:<?php echo $rows['gender'];?></li>
<li>年龄:<?php echo $rows['age'];?></li>
<li>年级:<?php echo $rows['grade'];?></li>
<li>班级:<?php echo $rows['class'];?></li>
<?php
}
}
?>
</ul>
//方法二
<?php
$sql = "select * from `student` where `age`='16';";
$rows = mysql_query($rs);
?>
<ul>
<?php
if(mysql_affected_rows()){
while ($rows=mysql_fetch_assoc($rs)){
?>
<li>姓名:<?php echo $rows['stuname'];?></li>
<li>性别:<?php echo $rows['gender'];?></li>
<li>年龄:<?php echo $rows['age'];?></li>
<li>年级:<?php echo $rows['grade'];?></li>
<li>班级:<?php echo $rows['class'];?></li>
<?php
}
}else {
echo "查无数据!";
}
?>
</ul>
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!