Jump to content


Photo

search?


  • Please log in to reply
2 replies to this topic

#1 AndrewBacca

AndrewBacca
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationIsle of Wight, Uk

Posted 30 April 2003 - 10:05 AM

what i want to do is allow users of my site it search my database, using keyword(s) or phrases?

for example, if they type \'planets\', then it will list any news/articles with the word \'planets\' in it. and if they enter \'\"jupiters moons\"\' (with the \"\") it will find only the records with \"jupiters moons\" some where in it.

im not sure how to do this, so any help would be great :)

cheers
Andrew
Join a brand new astronomical community @ Astro-space.net

#2 barbatruc

barbatruc
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationMontreal, Quebec, Canada

Posted 30 April 2003 - 12:04 PM

You can use the LIKE command first to get in touch with MySQL queries. You could have something like:

(suppose search keywords are posted into $_POST[\'search\'])
$keywords = $_POST[\'search\'];

if (!get_magic_quotes_gpc()) {

    $keywords = addslashes($keywords);

}

$sql = "SELECT * "

      ."FROM `articles` "

      ."WHERE title   LIKE \'%". $keywords ."%\' "

      ."   OR content LIKE \'%". $keywords ."%\' ";
(ensure $keywords is not empty!)

This query will return you all articles where the title or the content match keywords you can find in $_POST[\'search\']. Suppose you submitted \"jupiter moons\", this will generate SQL query:
SELECT * FROM `articles` WHERE title LIKE \'%jupiter moons%\' OR content LIKE \'%jupiter moons%\'
Character \"%\" is used as a wildcard for 1 or several characters. So if the title of an article is \"What the the names of jupiter moons\", this article will be matched. With the MySQL LIKE command, character \"_\" can be used as a single character wildcard.
Ref: http://www.mysql.com..._functions.html

For advanced search queries in medium or big tables, you should take a look at the MATCH command because this command is really intended to ease the job of the programmer when building a simple search engine:
Ref: http://www.mysql.com...ext_Search.html

JP.
Unfortunately, PHP \'empowered every moron with a copy of Windows notepad to be \"web programmers\". (...) Give PHP a real INFRASTRUCTURE. Use PEAR!!!\'

#3 AndrewBacca

AndrewBacca
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationIsle of Wight, Uk

Posted 30 April 2003 - 02:50 PM

cheers, ive got it working :D

Andrew
Join a brand new astronomical community @ Astro-space.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users