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
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 />";
}

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.