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.
2. Next we'll insert make a page where we can add people and information about them. Simply just call this file add.php.
Now onto the actual searching, create a php page called search.php and use this code.
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>