Hi All,
Simple search engine I built off my address book database. This script seaches my table by the last_name field.
This search is looking for an exact match.
First the form:
searchform.php
PHP Code:
<html>
<head><title>Search By last_name from db</title>
</head>
<body bgcolor=#ffffff>
<h2>Search last_name from db - exact match</h2>
<form method="post" action="search.php">
<table width=90% align=center>
<tr><td>search for:</td><td><input type=text name='search' size=60 maxlength=255></td></tr>
<td></td><td><input type=submit></td></tr>
</table>
</form>
<?php include ('links.x');?>
</body>
</html>
The search script:
search.php
PHP Code:
<?
if ($search) // perform search only if a string was entered.
{
$database="yourdatabase";
mysql_connect ("localhost", "yourusername", "yourpassword");
@mysql_select_db($database) or die( "Unable to select database");
$query = "select * from yourtable WHERE last_name ='$search'";
$result = mysql_db_query("$database", $query);
if ($result)
{
echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#CCCCCC>First Name</td>
<td align=center bgcolor=#CCCCCC>Last Name</td>
<td align=center bgcolor=#CCCCCC>Address</td>
<td align=center bgcolor=#CCCCCC>Phone</td>
<td align=center bgcolor=#CCCCCC>Cell</td>
<td align=center bgcolor=#CCCCCC>email</td>
<td align=center bgcolor=#CCCCCC>Notes</td>
</tr>";
while ($r = mysql_fetch_array($result)) { // Begin while
$name = $r["first_name"];
$last = $r["last_name"];
$address = $r["address"];
$phone = $r["phone"];
$mobile = $r["mobile"];
$email = $r["email"];
$notes = $r["notes"];
echo "<tr>
<td>$name</td>
<td>$last</td>
<td>$address</td>
<td>$phone</td>
<td>$mobile</td>
<td>$email</td>
<td colspan=2>$notes</td>
</tr>
<tr><td height=8 colspan=7 bgcolor=\"#ffffa0\"></td>
</tr>";
} // end while
echo "</table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";
}
?>
This search is looking for an wildcard match.
First the form:
searchwildform.php
PHP Code:
<html>
<head><title>Searching the DB</title>
</head>
<body bgcolor=#ffffff>
<h2>Search last_name from db - any match</h2>
<form method="post" action="searchwild.php">
<table width=90% align=center>
<tr><td>search for:</td><td><input type=text name='search' size=60 maxlength=255></td></tr>
<td></td><td><input type=submit></td></tr>
</table>
</form>
</body>
</html>
The wildcard search script:
searchwild.php
PHP Code:
<?
if ($search) // perform search only if a string was entered.
{
$database="yourdatabase";
mysql_connect ("localhost", "yourusername", "yourpassword");
@mysql_select_db($database) or die( "Unable to select database");
$query = "select * from yourtable WHERE last_name LIKE '%$search%'";
$result = mysql_db_query("$database", $query);
if ($result)
{
echo "Here are the results:<br><br>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#CCCCCC>First Name</td>
<td align=center bgcolor=#CCCCCC>Last Name</td>
<td align=center bgcolor=#CCCCCC>Address</td>
<td align=center bgcolor=#CCCCCC>Phone</td>
<td align=center bgcolor=#CCCCCC>Cell</td>
<td align=center bgcolor=#CCCCCC>email</td>
<td align=center bgcolor=#CCCCCC>Notes</td>
</tr>";
while ($r = mysql_fetch_array($result)) { // Begin while
$name = $r["first_name"];
$last = $r["last_name"];
$address = $r["address"];
$phone = $r["phone"];
$mobile = $r["mobile"];
$email = $r["email"];
$notes = $r["notes"];
echo "<tr>
<td>$name</td>
<td>$last</td>
<td>$address</td>
<td>$phone</td>
<td>$mobile</td>
<td>$email</td>
<td colspan=2>$notes</td>
</tr>
<tr><td height=8 colspan=7 bgcolor=\"#ffffa0\"></td>
</tr>";
} // end while
echo "</table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";
}
?>
link.x
PHP Code:
<ul>
<li><a href="searchform.php">Exact Match Query</a>
<li><a href="searchwildform.php">Any Match Query</a>
<li><a href="some_homepage.html">Home</a>
</ul>
Examples of Searches:
$variable = 'Simon';
SELECT * FROM table WHERE record = '$variable';
- will give everything that EXACTLY matches 'Simon'
SELECT * FROM table WHERE record LIKE '%$variable';
- will give everything that matches '*Simon' eg: 'bsdbdsgSimon'
SELECT * FROM table WHERE record LIKE '$variable%';
- will give everything that matches 'Simon*' eg: 'SimonSACBKJbcssacc'
SELECT * FROM table WHERE record LIKE '%$variable%';
- will give everything that matches '*Simon*' eg: 'bsdbdsgSimonDSVPDSVKDSVasda'
Hope this helps someone