Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Singular Searching

Status
Not open for further replies.

nyoung2005

Programmer
Aug 9, 2005
5
GB
Hi,

I am trying to get PHP to search my mySQL database. Everything is fine at the moment except for the query is not searching for singular items. For example if you type in mint it comes up with everything with mint in it but if mints is typed in it does not bring up everything with mint!

I have a feeling that this is more of a mySQL issue even though it is coded in PHP.

Can anyone tell me where I am going wrong in my query?

Many Thanks

PHP:
$searchString = $_POST['searchString']; 

include("dbProductsConnect.php"); 

$sql = "SELECT * FROM productlist WHERE title LIKE '%$searchString%'"; 

$rs=mysql_query($sql,$conn) or die("Could not execute query"); 

$noOfRecords = 0; 

$list = "<br/>"; 
$list .= "<hr>"; 

while($row= mysql_fetch_array($rs) ) 
{ 
$productID = $row["productID"]; 
$postedTitle = $row["title"]; 
$postedDescription = $row["description"]; 
$postedPrintArea = $row["printarea"]; 
$postedLeadTime = $row["leadtime"]; 
$postedFastTrack = $row["fasttrack"]; 
$postedCategory = $row["category"]; 
$postedMinQuantity = $row["minquantity"]; 
$imageDisplay = $row["image2"]; 

$noOfRecords++; 

$list .= "<span class=\"productCatalogueItems\"><b>Item " . $noOfRecords . ": </b></span>"; 

$list .= "<span class=\"pageMenuLinks\"><span class=\"productCatalogueItems\"><a href='product.php?cmd=product&productID=$productID&category=$postedCategory&title=$postedTitle&description=$postedDescription&image2=$imageDisplay&printarea=$postedPrintArea&minquantity=$postedMinQuantity&fasttrack=$postedFastTrack&leadtime=$postedLeadTime'>".$row["title"]."</span></a></span><br/><br/>"; 

$list .= "<p class=\"productCatalogueItems\">$postedDescription</p>"; 
$list .= "<hr>"; 
} 

if ($noOfRecords==0) 
{ 
echo "<p>Your search returned 0 products</p>"; 
echo("<p><a href='javascript:history.back(1);'>Back</a></p>"); 
} 
else if ($searchString == "") 
{ 
echo "<p>You did not type anything in the search box</p>"; 
echo("<p><a href='javascript:history.back(1);'>Back</a></p>"); 
} 
else 
{ 
echo("<span class=\"productCatalogueItems\">Below are your results for <b>$searchString</b>:</span><br/>"); 
echo($list); 
}
 
I do not understand the question. It appears to be obvious that:
%mint% will match anything that has mint in it.
%mints% will not match anything that has just mint in it as the compariosn for the 's' fails.
 
Hi - sorry to be a bit vague.

The idea is that I want to be able to type in "mints" into the search field.

The variable then gets passed into the SQL query and it brings up all products that have mint in the title.

Many Thanks
 
For any one that is interested - solved this problem using the strrchar and substr_replace methods. The code can be found below:



$searchString = $_POST['searchString'];
// take into account case sensitivity
$endLetterLC = strrchr($searchString, 's');
$endLetterUC = strrchr($searchString, 'S');


if ($endLetterLC == "s" || $endLetterUC== "S")
{
$searchString = substr_replace($searchString, "", -1);
}
 
Your code is clever, and it will work for a word like "mints".

However, I do not thing it will work for a word like "strings"


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214 - have tested it and it works fine - placed two items into my database called String and Strings and it found them both.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top