Page 1 of 1

I desperately need help with this mysql php search script...

Posted: Fri May 14, 2004 1:31 pm
by safewayclubcard
So here is what i need help with. I need to make a search engines for inventory items in a database and have them displayed in a table format.. but i cant seem to get the LIKE function to work properly. is there something wrong with my code?


<?php



$db = mysql_connect("host", "user", "pw");
mysql_select_db("show19",$db);

if ($search)
{
$search = trim($search);
$search = explode(" ", $search);
}


$selectquery = "SELECT * FROM INTERNET";


if (isset($search))
{
$selectquery .= " WHERE ";
for($i = 0; $i < count($search); $i++)
{
$selectquery .= "'ITEM DESCRIPTION' LIKE '%" . $search[$i] . "%'";
if ($i < count($search)-1)
{
$selectquery .= " AND ";
}
}
}
$result = mysql_query("$selectquery",$db);

if ($list = mysql_fetch_array($result)) {


echo "<table border=1>\n";

echo "<tr><td><b>SKU</td><td><b>Item</td><td><b>Price</b></td></tr>\n";

do {



printf("<tr><td>%s</td><td>%s</td><td>$ %s</tr>\n", $list["SKU"], $list["ITEM DESCRIPTION"], $list["LOW PRICE"]);

} while ($list = mysql_fetch_array($result));

echo "</table>\n";

} else {

echo "Sorry, no records were found!";

}



?>


results are never found.. here is how my database is layed out. the data is in a table called database called show19 in a table called INTERNET. I am trying to do a search through a varchar field called ITEM DESCRIPTION and spit out the results while showing the SKU, ITEM DESCRIPTION, and PRICE. All of the entrys in each field are all caps. Is this case sensitive? I have tried both ways and neither work. Is there something wrong in the query? Basically what i watn to happen is if a user types in: 'Pearl' it would spit back a result called USED PEARL SNARE or PEARL MAPLE SNARE. Any suggestions?

Posted: Fri May 14, 2004 3:27 pm
by lostboy
test the sql query by
1. Looking at it for correctness

Code: Select all

[b]echo $selectquery;

//now run it...remove the quotes around the $selectquery

$result = mysql_query($selectquery,$db) or die("Can't complete query because ". mysql_error());  //will give error messages

hmm

Posted: Fri May 14, 2004 4:09 pm
by safewayclubcard
i tested it for correctness, and made changes to the query where the search is..

$selectquery .= "'ITEM DESCRIPTION' LIKE '%" . $search[$i] . "%'";

when i did that, i still dont get anything....

is there anything im doing wrong?

Posted: Fri May 14, 2004 5:02 pm
by feyd
have you tried this:

Code: Select all

$selectquery .= "`ITEM DESCRIPTION` LIKE '%" . $search[$i] . "%'";