Trouble getting just one result.

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
PatJunkins
Forum Newbie
Posts: 9
Joined: Sat Feb 07, 2004 5:44 pm
Location: Maine

Trouble getting just one result.

Post by PatJunkins »

Can someone help a real newbie? I want to return a single exact result not all related results. What am I ding wrong?

$pn = ($pn == "" ? $pn = "%" : $pn = $pn);
$description = ($description == "" ? $description = "%" : $description = $description);

$query = mysql_query("SELECT part_number,description,price,weight FROM suzukiparts WHERE part_number LIKE '%$pn%' AND description LIKE '%$description%'")or die(mysql_error());

if (mysql_num_rows($query) > 0) {
while(list($o_part_number,$o_description,$o_price,$o_weight) = mysql_fetch_row($query)) {
if (stristr($o_description, "USE PART#")) {
list($p1,$part_num) = explode("# ",$o_description);
print "<body bgcolor=\"orange\"><table border=\"0\"><font face=\"arial\" size=\"-1\"><center><b><h4>Updated Part Number, Click on new number <a href=\"spart3.php?pn=$part_num\">$part_num</a>\n";
}
else {
print "<body bgcolor=\"yellow\"><table border=\"0\" width=\"100%\" align=\"left\"><tr valign=\"top\"><td width=\"20%\"><b><big>Part# </b></big> $o_part_number</td><td width=\"25%\"><b><big>Desc </big></b> $o_description</td><td width=\"15%\"><b><big>Price</big></b> $o_price</td><td width=\"5%\"><b><big>Quantity </big></b></td><td width=\"20%\"><form METHOD=\"POST\" ACTION=\"http://www.alpha-sports.com/cgi-bin/cart.cgi\" type=\"image\" SRC=\"images/add2cart.gif\" alt=\"Add to cart...\" name=\"I1\" target=\"_self\"><input type=\"hidden\" name=\"return\" value=\"http://www.alpha-sports.com/footer.html\"><input type=\"hidden\" name=\"price\" value=\"$o_price\"><input type=\"hidden\" name=\"name\" value=\"$o_description\"><INPUT TYPE=\"TEXT\" NAME=quantity VALUE=\"1\" SIZE=\"2\" MAXLENGTH=\"2\"><input type=\"hidden\" name=\"custom1\" value=\"$o_description\"> <input type=\"hidden\" name=\"custom2\" value=\"Stock# $o_part_number\"> <input type=\"hidden\" name=\"sh\" value=\"$o_weight\"> <INPUT TYPE=\"HIDDEN\" NAME=\"redirect\" TARGET=\"main1\" VALUE=\"1\"> <input type=\"hidden\" name=\"add\" value=\"1\"><input alt=\"Add to cart...\" name=\"I3\" src=\"images/add2cart.gif\" type=\"image\"><INPUT TYPE=\"hidden\" name=\"redirect\" value=\"2\"></form><td width=\"5%\"><form METHOD=\"POST\" ACTION=\"http://www.alpha-sports.com/cgi-bin/cart.cgi\" target=\"_top\"><input alt=\"Go to shopping cart checkout...\" name=\"I4\" src=\"new_images/checkoutButton_61x22.gif\" type=\"image\"></form></td></tr></table>\n";
}
}
} else {
print "<body bgcolor=\"red\"><table border=\"0\"><font face=\"arial\" size=\"-1\"><center><b><h4>Part number is no longer in the price book. Email us the part number, model and description for the price.";
}
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

If you want one exact result then you need to use the "=" sign, not LIKE. So change your query to:

Code: Select all

$query = mysql_query("SELECT part_number,description,price,weight FROM suzukiparts WHERE part_number = '$pn' AND description = '$description'") or die(mysql_error());
The part_number and description must be exact though.
PatJunkins
Forum Newbie
Posts: 9
Joined: Sat Feb 07, 2004 5:44 pm
Location: Maine

Post by PatJunkins »

Well I tried that and it can't find any record of it, though it will with the LIKE command. Is there a problem of spaces that need to be trimmed? or justification? I tried a few variations of = and then LIKE on the $pn and $description. I'm a little lost on this one.

TIA Pat
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try just usins part_number = '$pn' and take the description out, because doing that then someone would have to type exactly what the description is int he database...
PatJunkins
Forum Newbie
Posts: 9
Joined: Sat Feb 07, 2004 5:44 pm
Location: Maine

Post by PatJunkins »

Would work but the problem is sometimes that number shows up in the description as an alternate part number so I have to look for it in both places. How could I set it up to look in the pn first and then look for the string in the description if not there?
PatJunkins
Forum Newbie
Posts: 9
Joined: Sat Feb 07, 2004 5:44 pm
Location: Maine

Post by PatJunkins »

Anyone have any ideas or is this not possible without much trouble?
Post Reply