Jump to content

search database script


Orionsbelter

Recommended Posts

hi am making a website that then some seaches in the input box i'll search my database but i have no clue what the coding should be can anyone help,

 

just for an easy start say i have a username search and the field is called username and the table is users.

 

what would it look like to return all usernames like the search?

 

thanks

 

Link to comment
https://forums.phpfreaks.com/topic/125929-search-database-script/
Share on other sites

SQL queries have something called like, which allow for the user of a wildcard '%'. Wildcards will match all characters, or only 1 depending on the wild card. So if you wanted  to search for users with the search word anywhere in their name, you could do this:

 

$keyword = $_POST['keyword']; //This is the value from the search box

$query1   = "SELECT `username`,`id` FROM `users` WHERE `username` LIKE '%$keyword%'";
$execute1 = mysql_query($query1) or die("Oops, looks like there was an error<br /><br />" . mysql_error());

while ($i = mysql_fetch_row($execute1)) {
    echo $i[0] . "<br />";
}

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.