0

In my table i have a column for spot name (place name) in which i have a string
like - City Bank Building.

i have my user interface in which i have one search box in that user can enter any keyword for searching.

now i want to search that keyword from the table column spot name which is a string.

so basically i want to extract a substring from the string which is stored in the database.

for example,

City Bank Building - this is the database string
Bank - my search keyword

any combination of the string which contains "Bank" as a word in the string should be display in the list.

this is my code,

switch($search_by){
    case "spotname":
        $sql_where_clause = "sname LIKE ' %" . $search_keyword."%'";
        break;
}

anyone know how to do this?

Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
Arpi Patel
  • 775
  • 4
  • 10
  • 23
  • 2
    isn't this a basic select * from table_name where spot_name like "%_the_word_you_search_fo%" ? – mishu Nov 07 '11 at 10:59

2 Answers2

1

Using LIKE in your query will only work when your users enter a single word or exact phrase. A better way to do it is to use a full text index search:

CREATE TABLE places (
   id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
   name VARCHAR(200),
   FULLTEXT (name)
);

Then your query would look like this:

SELECT * FROM places 
WHERE MATCH (name) AGAINST ('SEARCH PHRASE');

Read more about full text search here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
0
<?php

    $query="Bank"; //or whatever you are looking for
    $mysql=mysql_connect();
    $result=mysql_query("SELECT `ColumnName` FROM `TableName` WHERE `ColumnName` LIKE '%$query%'", $mysql);
    mysql_close($mysql);

?>

The mysql query would become

SELECT `ColumnName` FROM `TableName` WHERE `ColumnName` LIKE '%Bank%'

Which will select anything that has Bank in it. So it will match all of the following

City Bank Building
Bank Building
Building Bank
Bank
BankBank
...and so on
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70