Database Forum
Web Database Register Members List Calendar FAQ

 
Go Back   Database Forum > Database Management > Cool Tips
User Name
Password

Reply
 
Thread Tools Search this Thread Display Modes
Cool Simple search engine
03-19-2005, 03:08 PM #1  

Niko
Member

 
Status: Offline
Posts: 34
Join Date: Mar 2005
Location: Canada

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

Last edited by Jenn; 06-25-2006 at 02:04 PM.. Reason: correct typo
Reply With Quote

Default
10-09-2005, 03:33 PM #2  

clifford
Senior Member

 
Status: Offline
Posts: 266
Join Date: Mar 2005
Location: San Fran, CA

In your project file simply enable quick search for your table. Then search you database anyway you wish. Remember you can always use the % sign for wildcard searces '%$search%' or %se or sea%h

You can also enable filters (and save filters in the plus version) for your project file for more defined queries.

hope this helps
Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 11:34 PM.

Powered by: vBulletin Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Database Forums

Help support our forum assistants and product development ->

go to top go to top