Database search function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluewaves
Forum Newbie
Posts: 7
Joined: Sat May 03, 2008 10:12 am

Database search function

Post by bluewaves »

I have a search box that is basically working, but it needs some tweaking.
I'd like the results page to echo, "Sorry, no results matched." or something
like that when there are no results.



This is the form for the text box:

Code: Select all

<form action="handle_keywords1.php" method="post">
          <b><p>Search Checks:</b> <input type="text" name="keyword" size="20" maxlength="40" /></p>
          <dd align="center"><input type="submit" name="submit" value="Search" /></dd>
        </form>
 
This is the handle_keywords1.php that handles it:

Code: Select all

 
<?php
 
 include('../../misc.inc');
  $cxn = mysql_connect($host,$user,$password)
         or die ("couldn't connect to server");
  mysql_select_db($database);
  $database = "voipvide_checks";
  $upperKeyword = strtoupper($keyword);  
  $query = "SELECT * FROM fourchecks WHERE upper(name) like '%$upperKeyword%' ORDER BY Name";
  $result = mysql_query($query)
            or die ("Couldn't execute query.");
 
 
 /* Display results in a table */
     echo "<center><table cellspacing='10'>";
  echo "<tr><td colspan='3'><hr /></td></tr>";
  while($row = mysql_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td width='300'><img src='$BigImage' border ='0' alt='$Name Bank Checks'><P><font size='2'>
            <a href='$Link'>$Name BANK CHECKS</a><br>
</td>\n
            <td width='100'></td>\n
                       </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table></center>\n";
 
?>
 
 
 
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: Database search function

Post by AXEmonster »

hi

Code: Select all

 
 
if(! $result){
 
$message = "no results";
 
}
 
 
// now you have set a condition and a message to display you can echo that $message anywhere on the page

Code: Select all

 
 
echo $message;
 
 
bluewaves
Forum Newbie
Posts: 7
Joined: Sat May 03, 2008 10:12 am

Re: Database search function

Post by bluewaves »

I'm such a beginner here. Where in my script do I put this:

Code: Select all

if(! $result){
 
$message = "no results";
 
}
 
And do I use this anywhere I want the results to show?

Code: Select all

<?php
 
echo $message;
 
?>
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: Database search function

Post by AXEmonster »

the first bit can go anywhere after your query

the next bit can go any where inside your HTML to display the message
bluewaves
Forum Newbie
Posts: 7
Joined: Sat May 03, 2008 10:12 am

Re: Database search function

Post by bluewaves »

Well...this is how i put the code:

Code: Select all

<table><tr><td><h1>Search Results For Bank Checks</h1><br>
<?php
 
echo $message;
 
?>
 
</td>
</tr></table>
<table>
<tr>
 
 
 
 
<td width="450"><font size="2" face="Arial">
 
 
<?php
 
 include('../../misc.inc');
  $cxn = mysql_connect($host,$user,$password)
         or die ("couldn't connect to server");
  mysql_select_db($database);
  $database = "voipvide_checks";
  $upperKeyword = strtoupper($keyword);  
  $query = "SELECT * FROM fourchecks WHERE upper(name) like '%$upperKeyword%' ORDER BY Name";
  $result = mysql_query($query)
            or die ("Couldn't execute query.");
  if(! $result){
 
  $message = "no results";
 
}
 
  $message = "Sorry, no matching results.";
 
 
 /* Display results in a table */
     echo "<center><table cellspacing='10'>";
  echo "<tr><td colspan='3'><hr /></td></tr>";
  while($row = mysql_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td width='300'><img src='$BigImage' border ='0' alt='$Name Bank Checks'><P><font size='2'>
            <a href='$Link'>$Name BANK CHECKS</a><br>
</td>\n
            <td width='100'></td>\n
                       </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table></center>\n";
 
 
 
 
 
?>
I still don't see the message though...by the way..thanks for helping me.
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: Database search function

Post by AXEmonster »

where is the first bit of the code i gave you
bluewaves
Forum Newbie
Posts: 7
Joined: Sat May 03, 2008 10:12 am

Re: Database search function

Post by bluewaves »

Line 30-32
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: Database search function

Post by AXEmonster »

and your query is working returning results to the page
bluewaves
Forum Newbie
Posts: 7
Joined: Sat May 03, 2008 10:12 am

Re: Database search function

Post by bluewaves »

yes...if there are results.
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: Database search function

Post by AXEmonster »

post all the code as you have it

php bit
remyx187
Forum Newbie
Posts: 8
Joined: Tue Apr 29, 2008 11:49 am

Re: Database search function

Post by remyx187 »

You can also do something like this after the mysql_query() function

Code: Select all

 
$found = mysql_num_rows($result);
 
if($found == 0){
 
echo "Search returned no results.";
 
}
 
bluewaves
Forum Newbie
Posts: 7
Joined: Sat May 03, 2008 10:12 am

Re: Database search function

Post by bluewaves »

O.k. That one worked like a charm. :D Bless your heart!!!

Thanks for hanging in there with me!
Post Reply