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

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.