使用mysql_num_rows时出错

时间:2010-10-20 06:25:29

标签: php mysql

  

可能重复:
  Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error

我试图做一个搜索引擎。但是,即时通讯面临这个错误..任何人都可以帮助..

错误:

警告:mysql_num_rows()期望参数1为资源,第90行的C:\ wamp \ www \ i-document \ search_admin.php中给出布尔值

提前谢谢。

Php代码:

<?php

//Get data    

$button = isset($_GET['submit']) ? $_GET['submit'] : '';          
$search = isset($_GET['search']) ? $_GET['search'] : '';       
$find = isset($_GET['find']) ? $_GET['find'] : '';      

if (!$button)
  echo "You didn't submit a keyword.";  
else  
{  
  if (strlen($search)<=1)  
    echo "Search term too short"; 
  else  
  {  
    echo "You searched for <b>$search</b><br /><br /><hr size='2'>";  

    //connect to database
    mysql_connect("localhost","root","");
    mysql_select_db("idoc");

    $query = mysql_query("SELECT * FROM document WHERE $search LIKE'%$find%'");
  }

 $run = mysql_query($query);

 $foundnum = mysql_num_rows($run);

 if ($foundnum==0)
    echo "No results found";
 else
 {
    echo "$foundnum results found!<p>"; 

    echo "<table border='1'>
    <th>File Reference No.</th>
    <th>File Name</th>
    <th>iShare URL</th>
    <th>Edit</th>
    <th>Delete</th>
    </tr>";
    while ($rows = mysql_fetch_assoc($run))  
    {
      echo "<tr>";
      echo "<td>". $rows['file_ref']  ."</td>";
      echo "<td>". $rows['file_name'] ."</td>";
      echo "<td><a href=" . $rows['url'] . ">" . $rows['url'] . "</a></td>";
      echo "<td><a href=edit_doc.php?id=" . $rows['id'] . ">Edit</a></td>";
      echo "<td><a href=delete.php?id=" . $rows['id'] . ">Delete</a></td>";
      echo "</tr>";
    }
    echo "</table>"; 
  }
}
?>

Html代码:

Htmak

<form id="form1" method="GET" action="search_admin.php">  
    <table>  
      <tr><td height="31" align="center" valign="middle" bgcolor="#990000"><span class="style1 style2">Search :</span></td></tr>  
      <tr><td><div align="left">Keyword :   
            <input name="search" type="text" id="search" size="40" /> in               
            <select name="field">
              <option value="file_name">File Name</option>     
              <option value="media_type">Media Type</option>  
            </select>  
          </div>  
            <input type = "submit" name="submit" value="search" />             
      </tr>  
    </table>  

4 个答案:

答案 0 :(得分:3)

你有这两行:

$query = mysql_query("SELECT * FROM document WHERE $search  LIKE'%$find%'");
$run = mysql_query($query);  

第一个在该字符串中运行查询并返回结果资源。在第二行中,将结果资源传递给mysql_query(),它以字符串作为参数。由于这可能无效,因此返回false。然后,您尝试询问有多少行为false。

答案 1 :(得分:0)

检查您的查询是否有效;错误消息表明mysql_query未返回有效的结果集。

答案 2 :(得分:0)

 $query = mysql_query("SELECT * FROM document WHERE $search  LIKE'%$find%'");
 //echo out construct  

 //this is setting run to, apparently a boolean, because you pass in the query result instead of a string
 $run = mysql_query($query);  

 //now you're passing a boolean into mysql_num_rows()
 //$foundnum = mysql_num_rows($run);  
 // instead try:
 $foundnum = mysql_num_rows($query);  

我认为这就是问题......

答案 3 :(得分:0)

我可以看到的一个问题是,只有当此条件为if (strlen($search)<=1)为假时才连接到数据库并构造查询。

如果条件成立怎么办?在这种情况下,您不构造查询,但仍然继续运行空查询。你需要解决这个问题。