Creating a Search Engine in PHP and MySQL
In this Tutorial We are Creating a Search Engine in PHP and MySQL By Which we can search Particular Data From the Database.
For Making this There are few Steps
- Create a Database in phpmyadmin
- Create a Table of names in the Particular Database
- Enter some Names.
Then Create a PHP Script For Connecting to The Database of name connect.inc.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php /*----------------- Connection To The database -----------*/ $conn_error = 'Could not connect.'; $mysql_host ='localhost'; $mysql_user ='root'; $mysql_pass =''; $mysql_db='a_database'; if(!@mysql_connect($mysql_host,$mysql_user,$mysql_pass)|| @mysql_select_db($mysql_db)) { die($conn_error); } ?> |
<?php
/*----------------- Connection To The database -----------*/
$conn_error = 'Could not connect.';
$mysql_host ='localhost';
$mysql_user ='root';
$mysql_pass ='';
$mysql_db='a_database';
if(!@mysql_connect($mysql_host,$mysql_user,$mysql_pass)||
@mysql_select_db($mysql_db))
{
die($conn_error);
}
?>
And Then Create another PHP Script For Creating a Search Engine in PHP Which Help in Showing The Searching Results of Search.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/*---------Creating a Search Engine in PHP and MySQL-------------*/ <?php require 'connect.inc.php'; //requiring connect.inc.php if (isset($_POST['search_name'])) { $search_name = $_POST['search_name']; if(!empty($search_name)) { if(strlen($search_name)>=4) { $query ="SELECT 'name'FROM 'names' WHERE 'name' LIKE '%" .mysql_real_escape_string($search_name)."%'"; $query_run = mysql_query($query); $query_num_rows = mysql_num_rows($query_run); if($query_num_rows>=1) { echo $query_num_rows.'Results Found:<br>'; While($query_row = mysql_fetch_assoc($query_run)) { echo $query_row['name'].'<br>'; } } else { echo'No result Found.'; } } else { echo 'Your Keyword must be 5 charachter or more'; } } } ?> <form action="SearchEngine.php" method="POST"> Name:<input type="text" name="search_name"><input type="submit" value="Search"> </form> |
/*---------Creating a Search Engine in PHP and MySQL-------------*/
<?php
require 'connect.inc.php'; //requiring connect.inc.php
if (isset($_POST['search_name']))
{
$search_name = $_POST['search_name'];
if(!empty($search_name))
{
if(strlen($search_name)>=4)
{
$query ="SELECT 'name'FROM 'names' WHERE 'name' LIKE '%"
.mysql_real_escape_string($search_name)."%'";
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
if($query_num_rows>=1)
{
echo $query_num_rows.'Results Found:<br>';
While($query_row = mysql_fetch_assoc($query_run))
{
echo $query_row['name'].'<br>';
}
}
else
{
echo'No result Found.';
}
}
else
{
echo 'Your Keyword must be 5 charachter or more';
}
}
}
?>
<form action="SearchEngine.php" method="POST">
Name:<input type="text" name="search_name"><input type="submit" value="Search">
</form>