Jump to content

Concatenated query string won't return result


captjohneast

Recommended Posts

Hello,

 

I have spent the past couple of days trawling the forums and web trying to find a solution to my problem. Can you help?

 

I am creating a query string by concatenating the results from a form submission. When I try parse the string it fails to return a result, but when I use the same string in the query browser, or using a string literal, I get the correct result. I have a feeling it is the way the concatenation is constructed.

 

CODE SNIPS:

 

// fails

$query = "WHERE ";

//form stuff...

$query = $query."accGal=1 AND ";

//form stuff...

$query = $query."minXP>01 AND ";

 

$query = $query."corpActive=1";

 

// fails

$query = 'SELECT count(*) FROM tblcorps '.$_SESSION['corpQuery']; // were session var is 'WHERE accGal=1 AND minXP>0 and corpActive=1'

 

echo $query // Returns exactly what I expect and can be used in the query browser with a successful result

 

$result = MYSQL_QUERY( $query);

 

// passes

$query = 'SELECT count(*) FROM tblcorps WHERE accGal=1 AND minXP>0 and corpActive=1'

 

$result = MYSQL_QUERY( $query);

 

END SNIPS

 

I have tried varios things with the concatenation such as:

 

using mysql_real_escape_string()

using " " instead of ' '

using " string $_SESSION['corpQuery'] string "

using a standard variable instead of a session variable

 

The various permitations do not work either.

 

Probably something really simple but I am too tired to see...

 

Thanks in advance.

John

 

Thanks all for the prompt reply, and the winner is ...

 

removing the first ; from the escape greater than sign had not effect (but still return the correct greater than symbol)  :)

 

Changing the concaternation to $query = "$query accGal=1 AND " - still created the correct string but did not return what I expect (still fails)

 

Barand! For telling the noob to follow good practise and handle exceptions...

 

The exception revieled that the > was being escaped for PHP to > and re-escape by MySQL back to >

 

Solution: didn't need to escape the Greater Than symbol in the first place.... :-[

 

Thanks for your help everyone.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.