PHP/MySQL Search Engine

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
In this tutorial I will show you how to make a fully function search for your database. This tutorial uses "LIKE $search" Why? Because some people will search with more than one word and if they search for say three words then it will search the database for those three words in exact order just how they searched it, which is no good if they searched keywords like "php database mysql" which would never be a sentence that you would find.

1. First we should create a table (to use as an example) for holding people's names and a little about them.

Code:
<?php
// set your infomation.
$dbhost='localhost';
$dbusername='username';
$dbuserpass='password';
$dbname = 'database';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

//create the table, customize it if you want
$query = 'CREATE TABLE people(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30) NOT NULL,
about TEXT NOT NULL)';
$result = mysql_query($query);
echo "Table Created!";
?>

2. Next we'll insert make a page where we can add people and information about them. Simply just call this file add.php.

Code:
<center>
<?php
// set your infomation.
$dbhost='localhost';
$dbusername='username';
$dbuserpass='password';
$dbname = 'database';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

if ($_POST['name']) {
$name = $_POST['name'];
if ($_POST['about']) {
$about = $_POST['about'];
$query = "INSERT INTO people (name,about) VALUES ('".$name."','".$about."')";
mysql_query($query)or die(mysql_error());
echo $name." was inserted successfully";
}else{
echo "About was left blank";
}
}
?>
<h2>Add</h2>
<form action='add.php' method='POST'>
Name: <input type='text' name='name' size='33' /><br />
About: <textarea name='about' cols='25' rows='5'/></textarea><br />
<input type='submit' value='Add!' />
</form>
<a href="search.php">Search for a person.</a>
</center>

Now onto the actual searching, create a php page called search.php and use this code.

Code:
<?php
// set your infomation.
$dbhost='localhost';
$dbusername='username';
$dbuserpass='password';
$dbname = 'database';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');

$keywords = explode(" ", $search);

$query = "SELECT id,name,about FROM people " .
"WHERE about LIKE '%".$keywords['0']."%'";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND about LIKE '%".$keywords[$i]."%'";
}

$result = mysql_query($query) or die(mysql_error());
?>
<center>
<form method="GET" action="search.php">
<b>Search:</b> <input type="text" name="search" size="20" />
<input type="submit" value="Search!" />
</form>
<table width="50%" style="border:1px solid #000000;">
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='border-bottom: 1px solid #000000;'>";
echo "<b>".$row['name']."</b>";
echo "<p>".$row['about']."</p>";
echo "</td>";
echo "</tr>";
}
?>
</td>
</tr>
</table>
<a href="add.php">Add a person to our index.</a>
</center>
 

Akkarin

New Member
Messages
1,654
Reaction score
0
Points
0
Another great and useful tutorials by slowpaul! Good job man!
 

n4tec

Active Member
Messages
3,312
Reaction score
0
Points
36
o0slowpaul0o, thanx for the tutorial once again.. You really have very good utorials.. Keep it up..
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
You shoudl try to teach "what is being actually done", so people that don't know a lot of PHP can learn.

Just a suggestion, other then that, it's a pretty cool idea.
 

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
I was recommended that to a friend. Asked him, should I post down verables and tell them what they mean, just so you can learn PHP.
 

bdweb

New Member
Messages
30
Reaction score
0
Points
0
Thanks for your nice tutorial
I have question in serch option

When I create a check box in a forum if the user did not select the check box click submit it shows me,
Notice: Undefined index: cgi in c:\program files\easyphp1-8\www\webhosting\view.php on line 9

I use this funtion to call the variable

$php = $HTTP_POST_VARS['php'];

view.php

Which command or fution shall I use for check box
Can any one help me about this

Thanks in adv
 

*=Matt=*

New Member
Messages
32
Reaction score
0
Points
0
about time lol I needed a tut like this for some search page. Thank You
 

Articz

New Member
Messages
864
Reaction score
0
Points
0
nice one there slow thx for shareing will have to try this on out
 
Top