results from full text help

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

User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

results from full text help

Post by C_Calav »

hi guys,

been playing around with the full text search in mysql and got that to work. now i want to use the search on my web page. so i wrote a little script to test things out and now im a little stuck!

how do i list the results that my query brings back?

heres what i have so far:

Code: Select all

include("db.php"); 
	
	if ($_SERVERї'REQUEST_METHOD'] == "POST") 
        {
    		$keyword = ($_POSTї"keyword"]);
	}

    $query = "SELECT P_Name, MATCH (P_Desc) AGAINST ('$keyword') FROM planes"; 
        
    $result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");

    $num_results = mysql_num_rows($result);

    echo '<p>Number of searches found: '.$num_results.'</p>';

    echo '<p>query string is: '.$query.'</p>';

    echo '<p>Result is: '.$result.'</p>';

    echo '<p>keyword is: '.$keyword.'</p>';


the output is: Resource id #4

just researching Resource id #4, looks like i have to add some more code.

can anyone help me with this Resource id #4 thing?


thanx!
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Cronikeys
Forum Commoner
Posts: 35
Joined: Sun Jan 16, 2005 9:14 am

Post by Cronikeys »

Code: Select all

$i=0;
while ($i < $num_results) &#123;
$P_Name=mysql_result($result,$i,"P_Name");
$P_Desc=mysql_result($result,$i,"P_Desc");
echo $P_Name." ".$P_Desc;
$i++;
&#125;
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx for your help scorphus & Cronikeys!

Cronikeys:

your code works great, but it just lists all the planes i have in my database.

how do i get just the planes that have a match for example in mysql to be displayed?
MATCH (P_Desc) AGAINST
('army')

0
0
3.0292830467224
0
2.0944106578827
here is my code so far:

Code: Select all

<?php  

    include("db.php"); 
	
	if ($_SERVER&#1111;'REQUEST_METHOD'] == "POST") 
        &#123;
    		$keyword = ($_POST&#1111;"keyword"]);
	&#125;

    $query = "SELECT P_Name, MATCH (P_Desc) AGAINST ('$keyword') FROM planes"; 
        
    $result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");

    $num_results = mysql_num_rows($result);

    $row = mysql_fetch_row($result); 

    $i=0; 
    while ($i < $num_results) &#123; 
    $P_Name=mysql_result($result,$i,"P_Name"); 

    echo $P_Name." ".$P_Desc;

    echo "<br />";    
 
    $i++; 
&#125;

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

might want to use something more like:

Code: Select all

SELECT * FROM `table` WHERE
MATCH( `column` ) AGAINST ('your stuff')
the "result" returned from mysql_query is merely a pointer to mysql result data. It is not a specific result per se.

You need to use one of the fetch functions, either in or out of a loop to get data: mysql_fetch_array()
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx feyd!

changed my sql to yours and works great with my code! :D
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey guys, just adding another field to search on..

this works :)

Code: Select all

SELECT * FROM planes WHERE MATCH (P_Desc) AGAINST ('$keyword')
this works :D

Code: Select all

SELECT * FROM planes WHERE MATCH (P_Name) AGAINST ('$keyword')
but this does not work.. why is that? :x

Code: Select all

SELECT * FROM planes WHERE MATCH (P_Name,P_Desc) AGAINST ('$keyword')
all tried with quotes around colum feilds etc

thanx very much!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe it's this :?:
Full-Text Search Functions wrote:For natural-language full-text searches, it is a requirement that the columns named in the MATCH() function be the same columns included in some FULLTEXT index in your table. For the preceding query, note that the columns named in the MATCH() function (title and body) are the same as those named in the definition of the article table's FULLTEXT index. If you wanted to search the title or body separately, you would need to create FULLTEXT indexes for each column.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

what does that mean?! 8O
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you look at the linked page, it shows an example table. The table has 2 full-text fields, both of which are declared in the same instance. Your table structure may need the same.. it's a total guess.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hey feyd,

you mean
FULLTEXT (title,body)
this bit here? when they create the table?

becuase i created the table first, then added FULLTEXT after by clicking the icons
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

right.. which may have screwed up the ability to search both fields at the same time. I don't know.. it's a thought.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ok i see what you mean now.. how can i go back and make them the correct way? or make them both FULLTEXT in one instance?

thanx!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ALTER TABLE maybe or recreate the table.. :?:
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

cheers man will do when get home!
Post Reply