Full-text search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zed420
Forum Commoner
Posts: 32
Joined: Wed Jun 04, 2008 11:24 am

Full-text search

Post by zed420 »

Hi All
I wonder if you could help me I'm trying to write a programm for Full-text search but I can not seem to get the query right. error says it can not connect to databse. Here is my code

Code: Select all

<?php
        $database = "sampledb";
        $user = "xxxxxxxx";
        $pass = "xxxxxx";
        $hostname = "localhost";
        $table = "itemtb";
    
    $db = mysql_connect("$hostname", "$user","$pass");
    mysql_select_db("$database");
 
   // If the form has been submitted with supplied keywords
   if (isset($_POST['keywords'])) {
      // Connect to server and select database
 
      $keywords = $_POST['keywords'];
 
      // Create the query
$query= "SELECT ItemName FROM itemtb WHERE MATCH(ItemName) against(`$keywords`)";
 $result = mysql_query($query)
       or die ("Couldn't execute query for collecting your data.");
    
?>
<TABLE BORDER=0 WIDTH=90% CELLSPACING=3 CELLPADDING=3 ALIGN=CENTER bgcolor="#99CC99">
<TR ALIGN=CENTER bgcolor="#CCFF66">
<td><font color=red><b>Item name</font></TD>
</tr>
<?
    while ($row = mysql_fetch_array($result))  {
    extract($row);
        echo "<tr align=center>
        <td>" . $row['ItemName'] . "</td>
        </tr>";
    }
?></table><?
   }
?>
<p>
<form name="action" id="action" method="post" action="<?=$_SERVER['PHP_SELF']?>">
      Keywords:<br />
      <input type="text" name="keywords" size="20" maxlength="40" value="" /><br />
      <input type="submit" value="Search!">
   </form>
 
On same script if I use different query it works fine for example if I take off MATCH and AGAINST and just leave it to show all ItemName it works.

Zed
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Full-text search

Post by VladSun »

What is the type of tables you use - MyISAM or InnoDB?
There are 10 types of people in this world, those who understand binary and those who don't
zed420
Forum Commoner
Posts: 32
Joined: Wed Jun 04, 2008 11:24 am

Re: Full-text search

Post by zed420 »

MyISAM

thanks for looking at my post
Zed
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Full-text search

Post by VladSun »

Hm, I see you are using backquotes in the query - try using single quotes.
Also, have a look at this:
http://www.petefreitag.com/item/477.cfm
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: Full-text search

Post by lonelywolf »

have you set FULLTEXT for columns you want to apply fulltext search?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Full-text search

Post by pickle »

If the error you're getting says you can't connect to the database, then it has nothing to do with your query, and everything to do with how you're trying to connect. Check your credentials.

Also, you don't need to pass your credentials in double-quotes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply