Jump to content

Search DB


djlfreak

Recommended Posts

I have 3 fields in tbl_product that I want to search : pd_dir, pd_cast, pd_name. It's a movie product table and I want users to be able to search the movies by name, director and cast. Not sure of code to display them as it involves getting an image from database. This is what I've tried but of course it's not working. Am I even close?

Search Products Table

$search = (isset($_GET['search'])) ? $_GET['search'] : '';
$sql = 'SELECT
pd_id    FROM        tbl_product
WHERE        
MATCH (pd_name, pd_dir, pd_cast) AGAINST ("' .
mysql_real_escape_string($search, $db) . '" IN BOOLEAN MODE)
ORDER BY        
MATCH (pd_name, pd_dir,pd_cast) AGAINST ("' .


mysql_real_escape_string($search, $db) . '" IN BOOLEAN MODE) DESC';
$result = mysql_query($sql, $db) or die(mysql_error($db));
if (mysql_num_rows($result) == 0) {
echo '<p><strong>No movies found that match the search terms.</strong></p>';
} else {   
while ($row = mysql_fetch_array($result)) {
output_movie($db, $row['pd_id']);    
   }
}
mysql_free_result($result);?>

 

Search Box

<form method="get" action="movie_search.php">   
<div style="padding-left: 25px;">    
<label for="search"><b>Search Movies</b></label>
<br>
<?php	echo '<input type="text" id="search" name="search" ';
if (isset($_GET['keywords'])) {   
echo ' value="' . htmlspecialchars($_GET['keywords']) . '" ';}
echo '/>';
?>
<input type="submit" value="Search" />    
</div>   
</form>

Display a movie from the database.

function output_movie($db, $pd_id) {  
  if (empty($pd_id)) {      
  return;   
}  
  $sql = 'SELECT
pd_name, pd_dir, pd_cast, pd_thumbnail         
FROM  
tbl_product        
WHERE
pd_id = ' . $pd_id;
$result = mysql_query($sql, $db) or die(mysql_error($db));
if ($row = mysql_fetch_assoc($result)) {        
extract($row);
echo '<h2>' . htmlspecialchars($pd_name) . '</h2>';
echo '<p>Dir: ' . htmlspecialchars($pd_dir) . '</p>';
echo '<p>Cast: ' . htmlspecialchars($pd_cast) . '</p>';
echo '$pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
} else {           
echo 'No Movies match your search.';      
  }    
}    
mysql_free_result($result);
}

Link to comment
https://forums.phpfreaks.com/topic/202924-search-db/
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.