Heim  >  Artikel  >  Backend-Entwicklung  >  php如何判断SQL语句的查询结果是否为空?

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

WBOY
WBOYOriginal
2016-06-23 14:03:241370Durchsuche

代码如下:

$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 "记录为空";}

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


回复讨论(解决方案)

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

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

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



这样试试

$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 查询所影响的记录行数。 

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

谢谢,对我有帮助

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:做什么项目比较好呢Nächster Artikel:iis服务器 隐藏index.php