警告:mysql_num_rows()期望参数1是资源。

时间:2014-04-10 07:18:18

标签: php html mysql-num-rows

代码工作正常,localhost上有几个页面,但这不能在服务器上运行,并显示像"mysql_num_rows() expects parameter 1 to be resource, boolean given in /home2/tp/public_html/ols/admin/delete-employee.php on line 23这样的排序警告。它总是让我感到困惑,因为它在某个页面上工作,并且在某些时候显示了一条让我很沮丧的警告信息。

请帮助..

<?php
    include('includes/start.php');
    require_once('includes/header.html');
?>
<?php


$page_title = 'View Employees';

require_once("../dbcon.php");
$query = "SELECT
    fname
    , lname
    , eid
    , fname
    , mname
    , lname
    , posid
    , date_added
FROM
    ccse.employee";
$result = @mysql_query($query);
$num = mysql_num_rows($result);
if($num > 0){
  echo "<p align='center'>There are $num employees found on the system!</p>\n";

if($result){

echo '<table align="center" border="1" bgcolor="lightgreen">
<tr>
<td colspan="111" align="center"><b>List of Employees</td></tr>
<tr>
<td align="center"><b>Employee ID</a></b></td>
<td align="center"><b>First Name</a></b></td>
<td align="center"><b>Middle Name</a></b></td>
<td align="center"><b>Last Name</a></b></td>
<td align="center"><b>Position</a></b></td>
<td align="center"><b>Date Added</a></b></td>
<td align="center"><b>Action</a></b></td>
</tr>';

$bg = '#eeeeee';
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
$bg = ($bg == '#eeeeee' ? '#ffffff' : '#eeeeee');


echo '<tr bgcolor="'.$bg.'">
<td align="left">'.$row['eid'].'</td>
<td align="left">'.$row['fname'].'</td>
<td align="left">'.$row['mname'].'</td>
<td align="left">'.$row['lname'].'</td>
<td align="left">'.$row['posid'].'</td>
<td align="left">'.$row['date_added'].'</td>
<td align="left"><a href="del_emp.php?id='.$row['eid'].'"><img src="../images/delete.jpg" width="50" height="20"></a></td>

</tr>';

}

echo '</table>';
//------------------End of Get list of Employees--------
mysql_free_result($result);
}else{
echo '<p class="error">There are currently no entered employees in the system!</p>';
/*echo '<p class="error">Data Can Not Be Retrieved!</p>';
echo '<p>'.mysql_error().'<br /><br />Query:'.$query.'</p>';*/
}
mysql_close();

if($num_pages > 1){
echo '<br /><p>';
$current_page = ($start/$display) + 1;

if($current_page !=1){
 echo '<a href="profile_emp.php?s='.($start - $display).'&np='.$num_pages.'$sort='.$sort.'" />Previous</a>';
 }

 for($i = 1; $i <=$num_pages; $i++){
  if($i !=$current_page){
  echo '<a href="emp-list.php?s='.(($display * ($i-1))).'&np='.$num_pages.'$sort='.$sort.'" />'.$i.'</a>';
  }else{
  echo $i.'';
  }
 }

 if($current_page !=$num_pages){
 echo '<a href="emp-list.php?s='.($start + $display).'&np='.$num_pages.'$sort='.$sort.'" />Next</a>';
 }
 echo '</p>';
 }
//------End of Paging Result----

}

?>
<?php
echo "<br>";echo "<br>";
include('../includes/footer.html');
?>

1 个答案:

答案 0 :(得分:0)

MySQL_num_rows失败的原因是因为$result可能只是false - 这发生在失败的查询中。使用MySQL_error()查找查询中的错误位置 - 可能是错误的数据库等等 - 我还建议远离MySQL_函数,因为它们已被弃用。请改用mysqli_