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
Share on other sites

You never check your query was successfull before attempting to use its results. Use....

 

<?php

  if ($numresults=mysql_query($query)) {
    $numrows=mysql_num_rows($numresults);
  } else {
    echo "Query failed<br />$query<br />" . mysql_error();
  }

?>

Link to comment
Share on other sites

perhaps it's something with my if else statement. i'm getting a "query was empty" statement from that error check.

 

also, when i insert any query by itself without the if-else then it works quite well.

 

sorry.

i'm rather new (not the first time you've heard that eh?)

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.