Skip navigation links

MySQL Forums :: PHP :: MySQL PHP Search Query Code


Advanced Search

MySQL PHP Search Query Code
Posted by: Dayse Mejias ()
Date: September 29, 2011 12:43AM

I'm not a programmer, I'm the site owner. I need to change MySQL query results to show results for the keywords as they are separated by commas. For example, if a user searches for: haunted house, I need the results to show all haunted house movies and not all movies that have the word "house" or "haunted" separately in the description or title. Right now I would get all movies that have the word "house" or "haunted" in the description or title.

This is the code that I found on my site that looks like the query:

<?php include("functions/globals.php"); ?>
<?php include("header.php"); ?>
<?php session_start(); ?>
<?php ob_start();//Required for the redirect to work?>

<SCRIPT LANGUAGE="JavaScript" SRC="java/ratings.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="java/jquery.js"></SCRIPT>

<div class="container">
<div class="contentcontainer">

<?php //include("sidebar.php"); ?>

<?php
$words = $_POST['search'];
OpenDatabase();
$words = mysql_real_escape_string($words);

$ids = GetIDsFromWords($words);//Get all of the Keyword ID's
$num = mysql_numrows($ids);
if($num > 0)
{
$num = mysql_numrows($ids);
$query = "SELECT movieid FROM moviewords WHERE wordid=";
for($x = 0; $x < $num; $x++)
{
if($x+1 == $num)
$query .= mysql_result($ids, $x, "id");
else
$query .= mysql_result($ids, $x, "id") . " OR wordid=";
}
$result = QuickQuery($query);
$num = mysql_numrows($result);

$movieids = array();
for($x = 0; $x < $num; $x++)
{
$movieids[$x] = mysql_result($result, $x, "movieid");
}

// $movieids = array_unique($movieids); <--An evil function that kills this.. perhaps in the future get it going properly

/* DEBUGGING STUFF
echo "Movie IDs returned size: " . sizeof($movieids) . "<br />";
for($x = 0; $x < sizeof($movieids); $x++)
{
echo($movieids[$x]) . "<br />";
}*/
$query = "SELECT * FROM movies WHERE MATCH(description, title) AGAINST('{$words}') OR id=";
for($x = 0; $x < sizeof($movieids); $x++)
{
if($x+1 == sizeof($movieids))
$query .= $movieids[$x];
else
$query .= $movieids[$x] . " OR id=";
}
$result = QuickQuery($query);
}
else
$result = QuickQuery("SELECT * FROM movies WHERE MATCH(description, title) AGAINST('{$words}')");
$num=mysql_numrows($result);
if($num)
include("sidebar.php");
$results = DisplayResults($result);
?>

</div><!--contentcontainer-->
</div><!--container-->

<?php include("footer.php"); ?>
</body>
</html>

Can anyone tell me what I have to change? Your help will be greatly appreciated.
Thanks



Edited 1 time(s). Last edit at 09/29/2011 02:52PM by Dayse Mejias.

Options: ReplyQuote


Subject Written By Posted
MySQL PHP Search Query Code Dayse Mejias 09/29/2011 12:43AM
Re: MySQL PHP Search Query Code Rick James 09/30/2011 07:41PM
Re: MySQL PHP Search Query Code Dayse Mejias 10/03/2011 03:44PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.