Jump to content

MySQL keyword search


The Little Guy

Recommended Posts

Anyone know how to do a keyword search?

 

Right now, I have this:

$q = $_POST['q'];
$strings = explode(" ",$q);
$str = preg_replace("/ /","%",$q);
$str = preg_replace("/%%/","%",$str);

 

if $q looked like this:

The dog jumped the fence

it would convert it to this:

The%dog%jumped%the%fence

 

$query = "SELECT * FROM news_news WHERE title LIKE '%$str%' OR content LIKE '%$str%' OR date LIKE '%$str%'";

 

That does an OK job, but How could I get it to do a keyword search where it treats every word as a keyword, and not as a string?

Link to comment
https://forums.phpfreaks.com/topic/59198-mysql-keyword-search/
Share on other sites

mysql> SELECT * FROM articles WHERE MATCH (title,body)

    -> AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);

+----+-----------------------+-------------------------------------+

| id | title                | body                                |

+----+-----------------------+-------------------------------------+

|  1 | MySQL Tutorial        | DBMS stands for DataBase ...        |

|  2 | How To Use MySQL Well | After you went through a ...        |

|  3 | Optimizing MySQL      | In this tutorial we will show ...  |

|  4 | 1001 MySQL Tricks    | 1. Never run mysqld as root. 2. ... |

|  6 | MySQL Security        | When configured properly, MySQL ... |

+----+-----------------------+-------------------------------------+

 

 

Link to comment
https://forums.phpfreaks.com/topic/59198-mysql-keyword-search/#findComment-294098
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.