php如何判断SQL语句的查询结果是否为空?

WBOY
Release: 2016-06-23 14:03:24
Original
1369 people have browsed it

代码如下:

$sql =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");$result=mysql_fetch_array($sql);if(!empty($result)){while($result=mysql_fetch_array($sql)){echo "hello word!"}if(!empty($result)){echo "记录为空";}
Copy after login

测试结果为:无论记录是否为空,都会输出"hello word!"与"记录为空",也就是两个条件都成立,这就让我很费解了,到底怎样判断一个SQL返回结果是否为空?


回复讨论(解决方案)

$result =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");if (mysql_num_rows($result) < 1) echo '记录集为空';
Copy after login

多谢前辈指点 ,小生刚学PHP 对很多函数不太了解。

$result =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");if(count($result)<0){echo "查询无数据!";}
Copy after login



这样试试

$result =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");
if(!mysql_affected_rows()){
    echo '没有记录';
}

一楼的方法已经测试通过 ,三楼的估计应该不行,count值怎么也不会小于0吧,换成1应该也可以,四楼的mysql_affected_rows函数还真没见过,改天也测试下。

说明
int mysql_affected_rows ([ resource $link_identifier ] )
取得最近一次与 link_identifier 关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。 

多谢各位了,有问题再请教各位 。。

谢谢,对我有帮助

对我有帮助,不错,拿回去试试

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!