GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2007, 08:03
Richardknox Richardknox is offline
Junior Member
 
Join Date: Nov 2006
Location: Michigan
Posts: 83
Richardknox is on a distinguished road

Need help with my Search Script


I need some help with my search script. I use this script to search for information based on location, and when I use Kansas, I also get Arkansas.

Can you show me what the problem is?

Here is the PHP script for the search feature.

PHP Code:

<?php

include "db_connection.php";

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect($dbhostname,$dbusername,$dbpassword); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db($dbname) or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select *
    from wcdatabase 
    where state like \"%$trimmed%\" 
  order by camname"; // EDIT HERE and specify your table and field names for the SQL query

 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
 echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
// echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
?> 
<span class="style7"><span class="style8"><?
echo "<p>Searched for: &quot;" . $var . "&quot;</p>"; ?>

  #2  
Old 11-May-2007, 10:21
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 924
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help with my Search Script


Yes, the 'like' operator is giving you exactly what you want [based on the use of 'like']...

Think about it...Arkansas and Kansas BOTH contain 'kansas', so you'll have to control the pattern-match, or do something else. This is the same as if a user entered 'or', then Oregon, Georgia, Florida, and others..., will match the result.

EDIT:
I may not offer the best solution, but a few ideas come to mind...
1. show the user ALL results of their match, and let them choose.
2. Offer a combo-box choice of states to search (then merely use the selection)
3. Provide a radio-button option as to whether the search is 'pattern' or 'exact' and then modify the select string to contain the like [ if a pattern ] or = [ if exact ].
4. Add another column that is the 2-letter state abbreviation, and offer to search that.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Web stats fcolor Web Design Forum 10 12-Jul-2007 08:48
Need Free Website Templates sam_dezine Web Design Forum 8 06-Sep-2006 03:25
Who Is A Web Designer ? alicehopkins Web Design Forum 1 13-Jun-2006 02:56
HTML Code amgujral Web Design Forum 0 07-Apr-2006 09:46
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 10:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:06.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.