Home  >  Article  >  Backend Development  >  高手帮小弟我看上这个代码,请帮忙注释上,多谢

高手帮小弟我看上这个代码,请帮忙注释上,多谢

WBOY
WBOYOriginal
2016-06-13 13:23:20649browse

高手帮我看下这个代码,请帮忙注释下,谢谢!

PHP code


//获取记录的总笔数

     public function num_rows($results) {  //$results参数是select_query($sql)的返回结果

         if(!is_bool($results)) {  //**********这个地方看不懂,为什么用is_bool()取反判断,这样有什么用?

             $num = mysql_num_rows($results);  

             $this->write_log("获取的记录条数为".$num);  

             return $num;  

         } else {  

             return 0;  

         }  

     }  



------解决方案--------------------
鉴于下面有 $num = mysql_num_rows($results);
所以 if(!is_bool($results)) { 是错误的
不是逻辑类型,并不能说就一定是资源类型
------解决方案--------------------
mysql_query() 执行出错时返回 false(bool) 值。就是判断这个用的。
if(!is_bool($results)) { 如果非bool 值,则说明query 执行成功了,否则失败了。
------解决方案--------------------
值得注意的是参数$results
看你的函数,$results参数应该是数据库查询的结果。
这样的话,用is_bool判断查询是否为假就没有什么好奇怪的了.....
Statement:
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