WHERE NOT IN Mysql statement not working.......
Posted: Thu Mar 13, 2008 2:27 pm
Hi,
I am trying to prevent products from appearing more than once. The page currently takes all products that are of the same type as the one shown on the page. It then displays 4 of them. Right now it can show duplicate items which looks silly (think of a page like "customers who bought this also bought: ")
I decided to fix this but running into a problem with MY SQL statement. There NOT IN doesn't seem to work like I thought. Is my logic off or am I just missing out how to use the NOT IN?
This is what the Where clause looks like at the end
Thanks,
I am trying to prevent products from appearing more than once. The page currently takes all products that are of the same type as the one shown on the page. It then displays 4 of them. Right now it can show duplicate items which looks silly (think of a page like "customers who bought this also bought: ")
I decided to fix this but running into a problem with MY SQL statement. There NOT IN doesn't seem to work like I thought. Is my logic off or am I just missing out how to use the NOT IN?
Code: Select all
/* $rs['pName'] is assigned elsewhere in the code, but i have echoed and it is what I want it to be */
$boughtString = "' '";
for($i=0;$i<=3;$i++)
{
echo '<td width="25%" align="center" valign="bottom">';
$query = " SELECT pName
FROM products
WHERE pType = '".$pType."' AND pDiscont = '1' AND pName NOT IN(".$boughtString.")";
$result= mysql_query($query);
if(strcmp($boughtString, "' '") == 0)
{
/*first entry */
$boughtString = "'".$rs['pName']."'";
}
elseif($i == 3)
{
/* last entry */
$boughtString .= ",'".$rs['pName']."'";
}
else
{
/* In between entries */
$boughtString .= ",'".$rs['pName']."'";
}
print_r( $boughtString.'<br>'); //print string after each run
Code: Select all
'Expanding Wooden Fence'
'Expanding Wooden Fence','Black Palm Wallet'
'Expanding Wooden Fence','Black Palm Wallet','Expanding Wooden Fence'
'Expanding Wooden Fence','Black Palm Wallet','Expanding Wooden Fence','Colour Changing LED Candles (Set of 2)'
Code: Select all
'See -Thru Shoe Store','Tungsten Carbide Edge Saw','Credit Card Case','Credit Card Case'