AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL
HTML Tutorial

How to create simple search engine using PHP and MySQL

Free Scripts » How to create search using PHP and MySQL

In this article I am going to explain how you can make a search engine based on MySQL and PHP.
I assume the reader is familiar with PHP programming and basic MySQL administration.

As a starting point, we have to have the information to look for stored in a table in our database. Let's take as an example a table with technical articles that we will call ARTICLES. This ARTICLES table will have at least two fields called TITLE and DEVELOPMENT, which will store the title of the article and its content respectively.

Form search

Search:

 

How to create a search form

Ahora diseñaremos una página con un formulario y una caja de texto para escribir la cadena de consulta busqueda y llamar al script de búsqueda buscar.php. Ejemplo:

Example

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="search.php" method="post">
Search: <input style="width:200px" type="text" name="term" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

In the file search.php is where the click of the question is.

You have to do the query that looks for the words and returns the matching records as a result. So far it seems easy and we could solve it like this:

SELECT * FROM ARTICLES WHERE DEVELOPMENT LIKE '% $ search%' OR TITLE LIKE '% $ search%'

Well, this query will show us all the articles that the search phrase appears in its title or in its content as we entered it. This is very limited since a change in the order of the words or a separating article will ruin our search by not showing the desired results.

Tags: How to create search option using PHP and MySQL, simple search in php with demo, simple search code in php, php search function, simple search engine in php, php search bar, php mysqli search database and display results, php mysql search database and display results,
how to create search engine in php and mysqli, simple site search php

 

How to create a PHP search script

The solution I have given to this problem of searches with MATCH ... AGAINST has been to check the number of words to search, using a simple search with LIKE in the case of a single word, and the MATCH ... AGAINST method in the case of several.

search.php

Example
<? php
// connection string
mysql_connect ("host", "user", "password");
// I MUST PREPARE THE TEXTS THAT I AM GOING TO SEARCH if the string exists
if ($ search <> '') {
// COUNT THE NUMBER OF WORDS
$ chunks = explode ("", $ search);
$ number = count ($ chunks);
if ($ number == 1) {
// IF THERE IS ONLY ONE SEARCH WORD, AN INSTRUCTION WITH LIKE IS ESTABLISHED
$ cadbusca = "SELECT REFERENCE, TITLE FROM ARTICLES WHERE VISIBLE = 1
AND DEVELOPMENT LIKE '% $ search%' OR TITLE LIKE '% $ search%' LIMIT 50 ";
} elseif ($ number> 1) {
// IF THERE IS A SENTENCE, THE ADVANCED SEARCH ALGORTIM OF MATCH AGAINST IS USED
// search for phrases with more than one word and a specialized algorithm
$ cadbusca = "SELECT REFERENCE, TITLE, MATCH (TITLE, DEVELOPMENT)
AGAINST ('$ search') AS Score FROM ARTICLES WHERE
MATCH (TITLE, DEVELOPMENT) AGAINST ('$ search') ORDER BY Score DESC LIMIT 50 ";
}
$ result = mysql ("teleformation", $ search cad);
While ($ row = mysql_fetch_object ($ result))
{
// We show the titles of the articles or whatever we want ...
$ reference = $ row-> REFERENCE;
$ title = $ row-> TITLE;
echo $ reference. "-". $ title. "<br>";
}
}
?>

 

Simple site search php

Search engine has become a useful tool in today's internet world.

Example

<?php
$server_name = "localhost";
$user_name = "root";
$password = "admin";
// Create connection myadmin
$conn = mysqli_connect($server_name, $user_name, $password);
// Check connection myadmin
if (!$conn) {
die("Connection failed to Database: " . mysqli_connect_error());
}
echo "Connected successfully to Database";
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search script</title>
</head>

<body>

<form action="#" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>

<strong>Go back tu search page <a href="http://www.agernic.com/search/">SEARCH AGAIN</a></strong>
<?php
$query = $_GET['query'];
// gets value sent over search form

$min_length = 3;
// you can set minimum length of the query if you want

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to &gt;

$query = mysqli_real_escape_string($query);
// makes sure nobody uses SQL injection

$raw_results = mysqli_query("SELECT * FROM newser
WHERE (`title` LIKE '%".$query."%') OR (`short_descript` LIKE '%".$query."%')
ORDER BY `blogid` ASC limit 20") or die(mysqli_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

if(mysqli_num_rows($raw_results) > 0){ // if one or more rows are returned do following

while($results = mysqli_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

echo "<p><strong>".$results['blogid'].". <a href='".$results['url_title']."'>".$results['title']."</a>
</strong><br /><font color='black'> ".$results['title']."</font><br /> <font color='green'>
".$results['title']."</font></p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "No results <br />";
echo ucfirst($query);
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>

</body>
</html>

 



How to create search option using PHP and MySQL, simple search in php with demo, simple search code in php, php search function, simple search engine in php, php search bar, php mysqli search database and display results, php mysql search database and display results, how to create search engine in php and mysqli, simple site search php
How to create simple search engine using PHP and MySQL - free scripts

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Send forgot password by mail or message ...
PHP MySQL simple login system...
Check for duplicate username live or ema...
How to create simple search engine using...
Diseño de sitio web CSS Layout...
Diseño de caja flexible CSS3...
Botones en CSS...
ID y Clase, Diferencias entre Class e Id...
Esquina redondeada CSS...
Tamaño de caja css box-sizing: border-b...
Alineación CSS...
Altura y Anchura CSS...
Bordes CSS...
Margen CSS...
Relleno CSS...
Modelo de caja CSS...
Tablas CSS...
Listas CSS...
Enlaces CSS...
Texto CSS...