Home > Backend Development > PHP Tutorial > php怎么判断SQL语句的查询结果是否为空

php怎么判断SQL语句的查询结果是否为空

WBOY
Release: 2016-06-13 12:51:18
Original
2807 people have browsed it

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

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

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


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

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



这样试试
------解决方案--------------------
$result =mysql_query( "select * from tv_video where title like '%$keyword%' limit $offset,$PageSize");
if(!mysql_affected_rows()){
    echo '没有记录';
}
------解决方案--------------------
说明
int mysql_affected_rows ([ resource $link_identifier ] )
取得最近一次与 link_identifier 关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。 

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