Jump to content

Simple Search Engine


Dysan

Recommended Posts

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

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

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.