[PHP] How to make a Search bar.

Status
Not open for further replies.
Web Developer
Joined
Oct 24, 2009
Messages
534
Reaction score
217
In this small tutorial I am going to show you how to code a PHP Search Bar.

First, you will need to set up an MySQL Database and connect to it.

Then, save the file below as 'search.php' or whatever you want.

PHP:
<?php
//connect to mysql
$db[host] = 'localhost';     //your db host (usually localhost)
$db[user] = 'root';          //the name of your mysql user
$db[pass] = 'lol';           //the password of the user connected to your database
$db[name] = 'phpsearch';     //the name of the database
$db[connect] = mysql_connect($db[host], $db[user], $db[pass]); //try connecting to the database
if(!$db[connect]){ //couldnt connect
	die('Could not connect to your MySQL DB'); //print what is in the quotes and exit the code
	exit;
}
mysql_select_db($db[name], $db[connect]); //select the database we want to use
//connect to mysql

if($_POST[search]){ //form submitted, clicked Submit Search
	$query = strip_tags(mysql_real_escape_string($_POST['query'])); //try to prevent sql injections
	if(!$query){ //not enterered a query
		echo 'You must enter a search query!';
	}else{
		//EDIT THIS ----------------------------------
		$table = 'users'; //the table you want to search
		$row = 'username'; //the row in which you want to search
		//EDIT THIS ----------------------------------
		$sql = mysql_query("SELECT * FROM `".$table."` WHERE `".$row."` LIKE '%".$query."%'"); //search query
		if($sql){ //no errors
			if(mysql_num_rows($sql) == 0){ //No results found.
				echo 'No results were found for <strong>'.$query.'</strong>';
			}else{ //one or more results have been found
				echo 'We have found <strong>'.mysql_num_rows($sql).'</strong> for <strong>'.$query.'</strong>.<br><br>
				<table>
					<tbody>
						<tr>
							<td><strong>User ID</strong></td>
							<td><strong>Username</strong></td>
						</tr>';
				while($r = mysql_fetch_array($sql)){ //get data of every user where their username is like the $query string
					$userid = $r["id"];
					$username = $r["username"];
					//lets put the part they searched in bold.
					$username = str_ireplace($query, '<strong>'.$query.'</strong>', $username);
					//lets put the part they searched in bold.
					echo '<tr>
						<td>'.$userid.'</td>
						<td>'.$username.'</td>
					</tr>';
				}
				echo '</tbody></table>';
			}
		}else{
			echo 'Sorry, an MySQL error occurred:<br><br>'.mysql_error(); //an error occurred, so echo it
		}
	}
}else{ //not clicked Submit Search, so echo the form
	echo '<h3>Search</h3>
	<br><br>
	<form method="post">
		<label for="q">Query:</label> <input type="text" name="query" id="q" value="m0nsta."><br>
		<input type="submit" name="search" value="Submit Search">
	</form>';
}
?>

Edit between the lines:
PHP:
//EDIT THIS ----------------------------------

Then visit the file and try searching.

Please note: I have not tested this, I have coded it from scratch but everything looks as if it should work.

Also:
If you search for 'm0n' and a user called 'm0nsta.' is in your database, it will be displayed as 'm0nsta.'
 
  • Like
Reactions: LMC
You didn't really teach anything, you just threw a glob of code there.
Explaining it bit by bit in detail would be better for those who don't know how to do it.
 
Status
Not open for further replies.
Back