Jump to content
xisto Community
Sign in to follow this  
iGuest

Fulltext mysql search engine

Recommended Posts

This is a script to search through items quickly...


<?php include 'db.php';/* you will need to connect to the database  ENTER LOG IN in the included file below :) */$page = $_GET['page'];/*--$page currently refers to the database table name - adjust as neccessary --*/$field1 = '';$field2 = '';/* ADJUST the above 2 values to be 2 columns in the mysql database you want to search */  if(empty($c)) { ?><form action="?page=<? echo $page; ?>&c=1" method=POST><b>Find Results with: </b><br>Any of these words: <input type="text" length=40 name="any"> <br>All of these words: <input type="text" length=40 name="all"> <br>None of these words: <input type="text" length=40 name="none"> <br><input type="submit" value="Search"></form><?   } else if($c) {       if((!$all) || ($all == "")) { $all = ""; } else { $all = "+(".$all.")"; }   if((!$any) || ($any == "")) { $any = ""; }    if((!$none) || ($none == "")) { $none = ""; } else { $none = "-(".$none.")"; }   $query = "       SELECT *,          MATCH($field1, $field2) AGAINST ('$all $none $any' IN BOOLEAN MODE) AS score           FROM $page        WHERE MATCH($field1, $field2) AGAINST ('$all $none $any' IN BOOLEAN MODE)";      $artm1 = MySQL_query($query);      if(!$artm1) {          echo MySQL_error()."<br>$query<br>";       }      echo "<b>Article Matches</b><br>";      if(MySQL_num_rows($artm1) > 0) {         echo "<table>";          echo "<tr><td>Score </td><td>$field1 </td><td> $field2</td></tr>";             while($artm2 = MySQL_fetch_array($artm1)) {            $val = round($artm2['score'], 3);            $val = $val*100;            echo "<tr><td>$val</td>";            echo "<td>{$artm2['field1']}</td>";            echo "<td>{$artm2['field2']}</td></tr>";/*ADJUST ABOVE - the 2 lines above assumes the data is coming form columns in the mysql database named field1 and field2 - so adjust to suit you. Also, $val is the score for returned items. The above is essentially the way the script is shown for returned values. */         }      echo "</table>";   }   else {       echo "No Results were found in this category.<br>";    }   echo "<br>";    }?>


db.php is shown below ENTER LOG IN:

<?//set database connection variables$hostname='localhost';$uname='';$password='';$database='';//establish database connection$connect = mysql_connect($hostname,$uname,$password);//display error if connection failsif ($connect==FALSE) {   print 'Unable to connect to database: '.mysql_error();} else {//select databasemysql_select_db($database);}?> 

Share this post


Link to post
Share on other sites

you'll need a different script to do that...this is just the search script to search through a table in the db...if your site is big enough to need a search engine, then you should really already have a way of adding items that fits this.Hope it helps

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.