djlfreak Posted May 26, 2010 Share Posted May 26, 2010 Hi all,it's me again, Could anyone help to figure out how to go about this. I'm burning the midnight oil on my project due tomorrow and my brain seems to have shut down. So 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. Thanks in advance [b]Search Products Table[/b] $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); ?> [b]Search Box[/b] <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> [b] Display a movie from the database. [/b] 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); } Quote Link to comment https://forums.phpfreaks.com/topic/202908-search-3-fields-in-db-and-display-results-with-image/ Share on other sites More sharing options...
kalivos Posted May 26, 2010 Share Posted May 26, 2010 MATCH...AGAINST is slow on larger datasets. For a school project, I don't think it's going to matter. Just something to keep in mind for later. You said it's not working. Have you echoed the SQL statements to verify that it's the statement you were expecting? I assume that a database connection is being made prior in the script. Have you tried executing your SQL directly with the database? Does it return the results you were expecting? What does the PHP script return? Error? Whitepage? More information is needed. -Kalivos Quote Link to comment https://forums.phpfreaks.com/topic/202908-search-3-fields-in-db-and-display-results-with-image/#findComment-1063446 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.