Jump to content

Multiple + partial search term query problem


eventide7

Recommended Posts

I apologize in advance for my utter noobness. I've been learning php in bits and pieces.

 

My database structure: 3 fields only, the relevant ones being "artist" and "title"

 

I made a search page (POETS Karaoke songs) that searches correctly, but only if the user inputs partial or complete words in the correct order, and the words only appear in one of the two fields.

 

I wanted to make it possible to find artist/title combination when multiple or partial words are input in any order. I did something wrong, in that my new coding returns the database regardless of the search terms. This broken page is here.

 

<form name="search" method="post" action="<?=$PHP_SELF?>">
<span>
<input type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</span>
</form>
<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<br><b>You searched for: " .$find;
echo "<hr>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>No search term entered. Please go back to the previous page and try again.";
exit;
}

// Begin Search
$searchQuery = ''; // search query is empty by default
$searchCondition = "(artist LIKE '%%' OR title LIKE '%%')";
$searchFieldName = 'artist'; // name of the field to be searched
$searchFieldName2 = 'title';
if(isset($find['text']))
{ // check if a query was submitted
$searchQuery = trim($find['text']); // getting rid of unnecessary white space
$searchTerms = explode(" ", $searchQuery); // Split the words
$searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
$searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
}
// End Search
$sql = "SELECT * FROM songs WHERE $searchCondition;";
// the rest is just database connection and retrieving the results
$dblink = mysql_connect("poetskaraokecom.ipagemysql.com", "poets", "guest");
mysql_select_db("poets_songs", $dblink);
$result = mysql_query($sql, $dblink);
echo "<ol>";
// Capture the result in an array, and loop through the array
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("<li>%s<font color='grey'> ... </font><i>%s</i></li>", $row[artist], $row[title]);
}

echo "</ol>";

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "No entries matched your query<br><br>";
}
}

?>

 

I haven't been able to determine whether my error is in my query of the database, or if it's in the return of the results. Any help would be much appreciated.

Thanks,

Cindy

Edited by eventide7
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.