Dysan Posted February 17, 2008 Share Posted February 17, 2008 Hi, How do I create a simple search engine, that allows the user to select a column to search (e.g. name) and then search the database and display the results. Link to comment https://forums.phpfreaks.com/topic/91504-simple-search-engine/ Share on other sites More sharing options...
uniflare Posted February 17, 2008 Share Posted February 17, 2008 im guessing you want a very simple search that returns user information. You would use a form with a drop-down for the fieldname (ie: Name) and a textbox for the search parameters (ie: John). Then your script would collect the $_POST variables from your form, Query the Database like so: mysql_query("SELECT * FROM `user_table` WHERE `htmlentities($_POST['fieldname'])`='htmlentities($_POST['SearchString'])';"); htmlentities() function is there to help prevent mysql injection attacks, never use $_POST or $_GET or ANY Raw User submitted data in a mysql query. This may not be sufficient for production scripts. NOTE: You may also want to lookup the LIKE mysql syntax and % Wildcards for non-exact matches.ie searching "Joe" will match "Joey" in database. --- To display the results simply you can use: Echo("Matches:<br />"); While($Row = mysql_fetch_array($Query_Result)){ Echo "Name: ".$Row['Name']."<br />"; Echo "Email: ".$Row['Email']."<br />"; Echo "ID: ".$Row['id']."<br />"; Echo "<hr>"; } So all Together the script could be: // Connect to mysql/Database $Result = mysql_query("SELECT * FROM `user_table` WHERE `htmlentities($_POST['fieldname'])`='htmlentities($_POST['SearchString'])';"); Echo("Matches:<br />"); While($Row = mysql_fetch_array($Query_Result)){ Echo "Name: ".$Row['Name']."<br />"; Echo "Email: ".$Row['Email']."<br />"; Echo "ID: ".$Row['id']."<br />"; Echo "<hr>"; } Hope this helps Link to comment https://forums.phpfreaks.com/topic/91504-simple-search-engine/#findComment-468713 Share on other sites More sharing options...
Chris92 Posted February 17, 2008 Share Posted February 17, 2008 To search through one column you could try: mysql_query("SELECT `name` FROM `table` WHERE `name` LIKE '%{$_POST['search']}%'"); To search through more than one at the same time: mysql_query("SELECT * FROM `table` WHERE MATCH (`name`, `column`) AGAINST ('{$_POST['search']}')"); Link to comment https://forums.phpfreaks.com/topic/91504-simple-search-engine/#findComment-468715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.