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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.