Jump to content


Photo

PHP/MySQL SELECT most similar (help me)


  • Please log in to reply
4 replies to this topic

#1 TitusRevised

TitusRevised
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 24 July 2006 - 02:31 AM

Is there an sql function to SELECT any rows in a table most similar to a field... like instead of SELECT * FROM table WHERE field LIKE '%$string%' which finds a row with keyword $string, so if $string is "hi my name is" but there isnt a row like but there is a row "hello my name is" it would pull that row because its most similar.

PHP example:
<?php
$string = trim(strip_tags(addslashes($_POST['usermsg'])));
$sql = mysql_query("SELECT * FROM table WHERE field = '%$string%");
// This finds me a row where field "field" has the $string in it exactly somewhere
?>
How can I make it so that if it's not exact it pulls out the row with the field "field" most similar to $string?

Your great wisdom and intelligence on the matter to assist me is appreciated greatly.

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 24 July 2006 - 03:03 AM

is this a text search?
Life's too short for arguing.

#3 TitusRevised

TitusRevised
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 24 July 2006 - 04:43 AM

is this a text search?

yes, $string is a string...text

#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 24 July 2006 - 06:50 AM

you might want to look at "match .. against()" in mysql manual
Life's too short for arguing.

#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 24 July 2006 - 07:02 AM

Yes, you can use full-text searching. If, this is manditory, you're able to add a fulltext index on the column you want to search.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users