query not returning expcted results

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
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

query not returning expcted results

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: query not returning expcted results

Post 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...
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

Re: query not returning expcted results

Post 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.
Post Reply