Jump to content

Recommended Posts

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 :confused:

 

  
[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);
}

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

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.