Jump to content

mysql_num_rows error


drawrof

Recommended Posts

i'm making a simple search which will search certain parts of a db depending on what the person wants.

 

they choose whether they want to search bands, title, or lyrics through and option menu.

 

but i keep getting this:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ihear6/public_html/test.php on line 107

 

here's the problem area in the php

 

// Build SQL Query  
if ( $field == "lyrics" ) {

 $query = "SELECT lyrics,  
               MATCH(lyrics)  
               AGAINST (\"%$trimmed%\" IN BOOLEAN MODE) AS score FROM songs 
               WHERE MATCH(lyrics)  
               AGAINST (\"%$trimmed%\" IN BOOLEAN MODE) ORDER BY score DESC";
     	   
} elseif ($field == "band") {

$query = "select * from songs WHERE band LIKE '$trimmed'"; 

} elseif ($field == "title") {

$query = "select * from songs WHERE title LIKE '$trimmed' || filename LIKE '$trimmed'"; 

} else {

echo "<p>uh oh! there seems to be a problem, dear.<p>";

}

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/56958-mysql_num_rows-error/
Share on other sites

here's the full script.

 

 

<html><form name="form" action="test.php" method="get">
  <div align="center">
    <p>
      <input name="q" type="text" value="soulmates!">
      by
      <select name="field">
        <Option VALUE="band">band</option>
        <Option VALUE="title">song title</option>
        <Option VALUE="lyrics">lyrics</option>
      </select>
      <input name="Submit" type="submit" class="submit" value="search!">
      </p>
  </div>
</form></html>


  <?php

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $search = trim($var); 
  $trimmed="%".$search."%"; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>you didn't enter anything useful!</p>";
  exit;
  }

//connect to db
mysql_connect("localhost","user","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("ihear6_songs") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
if ( $field == "lyrics" ) {

 $query = "SELECT lyrics,  
               MATCH(lyrics)  
               AGAINST (\"%$trimmed%\" IN BOOLEAN MODE) AS score FROM songs 
               WHERE MATCH(lyrics)  
               AGAINST (\"%$trimmed%\" IN BOOLEAN MODE) ORDER BY score DESC";
     	   
} elseif ($field == "band") {

$query = "select * from songs WHERE band LIKE '$trimmed'"; 

} elseif ($field == "title") {

$query = "select * from songs WHERE title LIKE '$trimmed' || filename LIKE '$trimmed'"; 

} else {

echo "<p>uh oh! there seems to be a problem, dear.<p>";

}

$numresults=mysql_query($query);
    $numrows=mysql_num_rows($numresults);

// If we have no results...

if ($numrows == 0)
  {
  echo "<p>sorry, dear! i couldn't find anything matching your search!</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>you searched for: <strong>" . $var . "</strong></p>";

// begin to show results set
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["title"];
  $folder = $row["folder"];
  $band = $row["band"];
  $filename = $row["filename"];

  echo "<p><strong>$count) </strong><a class=\"songs\" href=\"http://songs.iheartadelie.org/songs/$link\">$title</a></p>" ;
  $count++ ;
  }

//break before paging
  echo "<br/>";  
?>

Link to comment
https://forums.phpfreaks.com/topic/56958-mysql_num_rows-error/#findComment-281372
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.