PDA

View Full Version : PHP Search



MrCraig
06-08-2007, 11:17 AM
How is it i would code something so that it searches a database for words (basically a search bar)?

Blob
06-08-2007, 11:22 AM
$sql = mysql_query( "SELECT * FROM `database` WHERE `fieldname` LIKE '%' . $search . '%' ");

should work.

MrCraig
06-08-2007, 11:23 AM
$sql = mysql_query( "SELECT * FROM `database` WHERE `fieldname` LIKE '$search' ");

should work.

Thanks, Didnt know about the LIKE command :)

+REP if i can..

EDIT: Cant..

RichardKnox
06-08-2007, 11:24 AM
You don't need to use the % :P
Plus if you want to search multiple fields use this:



$sql = mysql_query( "SELECT * FROM `database` WHERE `fieldname` LIKE $search OR `fieldname2` LIKE $search OR `fieldname3` LIKE $search");

etc

Blob
06-08-2007, 11:25 AM
You don't need to use the % :P
Plus if you want to search multiple fields use this:



$sql = mysql_query( "SELECT * FROM `database` WHERE `fieldname` LIKE $search OR `fieldname2` LIKE $search OR `fieldname3` LIKE $search");

etc

If you have % it allows to search for the search to ignore the leading characters and the characters following the search string.

See

http://www.weberdev.com/ViewArticle.php3?ArticleID=81

Want to hide these adverts? Register an account for free!