search bar in php mysqli

Solutions on MaxInterview for search bar in php mysqli by the best coders in the world

showing results for - "search bar in php mysqli"
Sergio
14 Jul 2018
1<?php
2	mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
3	/*
4		localhost - it's location of the mysql server, usually localhost
5		root - your username
6		third is your password
7		
8		if connection fails it will stop loading the page and display an error
9	*/
10	
11	mysql_select_db("tutorial_search") or die(mysql_error());
12	/* tutorial_search is the name of database we've created */
13?>
14
15<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
16<html xmlns="http://www.w3.org/1999/xhtml">
17<head>
18	<title>Search results</title>
19	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
20	<link rel="stylesheet" type="text/css" href="style.css"/>
21</head>
22<body>
23<?php
24	$query = $_GET['query']; 
25	// gets value sent over search form
26	
27	$min_length = 3;
28	// you can set minimum length of the query if you want
29	
30	if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
31		
32		$query = htmlspecialchars($query); 
33		// changes characters used in html to their equivalents, for example: < to >
34		
35		$query = mysql_real_escape_string($query);
36		// makes sure nobody uses SQL injection
37		
38		$raw_results = mysql_query("SELECT * FROM articles
39			WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());
40			
41		// * means that it selects all fields, you can also write: `id`, `title`, `text`
42		// articles is the name of our table
43		
44		// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
45		// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
46		// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
47		
48		if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
49			
50			while($results = mysql_fetch_array($raw_results)){
51			// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
52			
53				echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
54				// posts results gotten from database(title and text) you can also show id ($results['id'])
55			}
56			
57		}
58		else{ // if there is no matching rows do following
59			echo "No results";
60		}
61		
62	}
63	else{ // if query length is less than minimum
64		echo "Minimum length is ".$min_length;
65	}
66?>
67</body>
68</html>
Loïc
11 Apr 2018
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4	<title>Search</title>
5	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6	<link rel="stylesheet" type="text/css" href="style.css"/>
7</head>
8<body>
9	<form action="search.php" method="GET">
10		<input type="text" name="query" />
11		<input type="submit" value="Search" />
12	</form>
13</body>
14</html>