drawrof Posted June 24, 2007 Share Posted June 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 24, 2007 Share Posted June 24, 2007 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(); } ?> Quote Link to comment Share on other sites More sharing options...
drawrof Posted June 24, 2007 Author Share Posted June 24, 2007 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?) Quote Link to comment Share on other sites More sharing options...
trq Posted June 24, 2007 Share Posted June 24, 2007 Where do you define $field? Quote Link to comment Share on other sites More sharing options...
drawrof Posted June 24, 2007 Author Share Posted June 24, 2007 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/>"; ?> Quote Link to comment Share on other sites More sharing options...
trq Posted June 24, 2007 Share Posted June 24, 2007 $field needs to be $_GET['field']. Quote Link to comment Share on other sites More sharing options...
drawrof Posted June 24, 2007 Author Share Posted June 24, 2007 ahh jeez. that's embarrassing. thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.