2009年3月23日星期一

php mysql mysql_fetch_array当结果不存在的时候 当结果为空的时候

HTML code
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 6 in C:\AppServ\www\back\class\class.read.php on line 137



下面这个就是在第137行:

PHP code
//====================输出 单个文章 的 页面标题 =======================     
function readtitle()     {         if(!is_numeric($this->rid)){         echo "<center>对不起!网址中的ID必须为整数!<br>".__BACK__."</center>";         exit;         }         $query_pagetitle="select `rtitle` from `read` where `rid`=".$this->rid;         $result_pagetitle=mysql_query($query_pagetitle);     if($result_pagetitle==false){                  $pagetitle="文章不存在!出错了!";              }else{         $pagetitle=mysql_result($result_pagetitle,0,"rtitle");//[b]这个是第137行[/b]         $pagetitle=htmlspecialchars($pagetitle);         $updatehit=new read($this->rid,'','','','');         $updatehit->updatehit();     }         return $pagetitle;     }


实际上总共三种情况

PHP code (正确的解答)
$query_pagetitle="select `rtitle` from `read` where `rid`=".$this->rid; $result_pagetitle=mysql_query($query_pagetitle); if($result_pagetitle){ $pagetitle='数据库语句出错了!'; }elseif($array=mysql_fetch_array($query)){ $pagetitle=$array['rtitle']; }else{ $pagetitle='文章不存在!'; } 

在PHP手册的用户已经说得很清楚,在http://cn.php.net/manual/zh/function.mysql-result.php,的留言的第一条:

mysql_result() will throw E_WARNING if mysql_query returns 0 rows. This is unlike any of the mysql_fetch_* functions so be careful of this if you have E_WARNING turned on in error_reporting(). You might want to check mysql_num_rows() before calling mysql_result()

当mysql_query返回0条记录时,就会产生E_WARNING级的错误,注意在mysql_result前用mysql_num_rows() 检查,推荐使用mysql_fetch_xxxx什么的函数


没有评论: