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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
safewayclubcard
Forum Newbie
Posts: 2
Joined: Fri May 14, 2004 1:31 pm

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

Post 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?
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post 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
safewayclubcard
Forum Newbie
Posts: 2
Joined: Fri May 14, 2004 1:31 pm

hmm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you tried this:

Code: Select all

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