Jump to content

[SOLVED] Full text search


twsowerby

Recommended Posts

Hi I'm really struggling with this full text search stuff.

 

I'm trying to get this search form:

 

echo "<form action='scripts/results.php?query=1' method='post'>";
echo "<input type='text' name='keyword'>";
echo "<input type='submit' value='Search!'>";
echo "</form>";

 

To pass the keyword to a script that will then query the database and return results. So far I've got this:

 

<?php


	$keyword=$_POST['keyword'];

        $sql = "select *, match(title, post) against('$keyword') as score from forum_posts where match(title, post) 
	against('$keyword') order by score desc";
        $rest = MySQL_query($sql);

echo "<table>";
echo "<tr><td>SCORE</td><td>TITLE</td><td>ID#</td></tr>";

        while($row = MySQL_fetch_array($rest)) {
            echo "<tr><td>{$sql2['score']}</td>";
            echo "<td>{$sql2['title']}</td>";
            echo "<td>{$sql2['id']}</td></tr>";
        }
        echo "</table>";

?> 

 

I'm still pretty new to php so I would be grateful if you could fully explain what I am meant to do for this to work.

 

Thanks,

 

Tom

 

 

Link to comment
Share on other sites

Try This:

 

<?php
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM files WHERE MATCH(title, post) AGAINST ('$keyword' IN BOOLEAN MODE) ORDER BY score DESC";

$sql = mysql_query($query)or die(mysql_error()); 
$result_count = mysql_query("SELECT FOUND_ROWS()")or die(mysql_error());
$total = mysql_fetch_array($result_count);
$totalrows = $total[0];
?>

Link to comment
Share on other sites

Give this a whirl:

 

<?php
$keyword=$_POST['keyword'];  // The keyword into a variable

// Make a safe query string
$query = sprintf("SELECT SQL_CALC_FOUND_ROWS * FROM files WHERE MATCH(title, post) AGAINST ('%s' IN BOOLEAN MODE) LIMIT 20", mysql_real_escape_string($keyword));

// Query the string
$sql = mysql_query($query)or die(mysql_error()); 
// Query the total number of rows if LIMIT was not set
$result_count = mysql_query("SELECT FOUND_ROWS()")or die(mysql_error());
// Return total number of rows into an array
$total = mysql_fetch_array($result_count);
// set the first value of the array (total number of rows) into a variable
$totalrows = $total[0];

// Now loop through your query
echo 'Found <strong>'.$totalrows.'</strong> Results';
while($row = mysql_fetch_array($sql)){
     echo '<p>'.$row['title'].'</p>';
}
?>

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.