tags:

views:

54

answers:

4

Hi, Im trying to do a search engine. However, im facing this errors.. Anyone can help..

Error :

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\i-document\search_admin.php on line 90

Thankz in advance.

Php Code :

<?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 Codes:

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>  
A: 

Check that your query is working; the error message indicates that mysql_query doesn't return a valid result set.

Extrakun
+3  A: 

You have these two lines:

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

The first runs the query in that string and returns a result resource. In the second line, you pass the result resource to mysql_query(), which takes a string as its argument. Since that can't possibly be valid, it returns false. You then try to ask how many rows are in false.

Dan Grossman
Moreover, if the "no keywords submitted" path is taken, then the first query call doesn't get executed, and the $run version gets executed with $query = NULL
Marc B
A: 
 $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);  

I think thats the problem...

Andy Groff
A: 

One problem I can see is that you connect to DB and construct the query only when this condition
if (strlen($search)<=1) is false.

What if that condition is true? In that case you don't construct the query but you still go ahead run an empty query. You need to fix that.

codaddict