Page 1 of 1

query not returning expcted results

Posted: Fri Aug 13, 2010 10:42 am
by scifirocket
first off, here is the code i'm using:

Code: Select all

$w=array(); 
if (!empty($RPrice)) $w[]="price='".mysql_real_escape_string($RPrice)."'";  
if (!empty($RType)) $w[]="type='".mysql_real_escape_string($RType)."'";
if (!empty($RRAtmosphere)) $w[]="atmosphere='".mysql_real_escape_string($RAtmosphere)."'";
if (!empty($RArea)) $w[]="area='".mysql_real_escape_string($RArea)."'";

 
if (count($w)) $where="WHERE ".implode(' AND ',$w); else $where=''; 
$sql="SELECT * from Foodlist $where"; 
let's assume the following about the database:

foo1
atmosphere = dark
area = north

foo2
atmosphere = light
area = south

if i run a query (with price and type being empty) with the parameters dark and south, it will return foo2 (it want it to return nothing). where is my logic error?

if you need more information to clarify things, please let me know.

Re: query not returning expcted results

Posted: Fri Aug 13, 2010 11:07 am
by mikosiko
two observations:

1)
scifirocket wrote:if (!empty($RRAtmosphere)) $w[]="atmosphere='".mysql_real_escape_string($RAtmosphere)."'";
the variable name $RRAtmosphere seems to be incorrect.

2) I don't see in your code a mysql connection active (granted you could be showing only part of your code)... from the mysql_real_escape_string manual:
"A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned. If link_identifier isn't defined, the last MySQL connection is used."
check the $w content before the select...

Re: query not returning expcted results

Posted: Fri Aug 13, 2010 11:16 am
by scifirocket
lol. that variable mistype was the problem. i was only showing part of the code, i should have said that. i can't believe i missed that variable.