mysql query string in link using urlencode


I have a sql query string that looks something like this:

SELECT * FROM listing_table WHERE (price>=0) AND (price<=1000000) AND (area_name='Town')

For paging through the results I have this: $search_url = urlencode($query); and then have a number of number links for page 2,3 4 and next previous and first last, as is pretty standard.

However when I click the next page now I get an error such as this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'Town\') ORDER BY at line 1

So the ' mark around the search criteria obviously isn't correct.  If I remove that part of the query string so that the search is only based on numerical criteria it works fine.

So my question is how do I properly make this search string useable in a url?

Thanks very much.

Schuyler KuhlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aaron TomoskyDirector of Solutions ConsultingCommented:
Nono. Don't put the whole query in the URL. Just put your variables in the URL. Like .com?low=0&high=100000
then in you code $low = $_get['low']
then build the query wrapping all your variables in mysqlrealescapestring.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Schuyler KuhlAuthor Commented:
Thank you.

Ok I am following you except for the last part about "wrapping all your variables in mysqlrealescapestring."

It is too bad though because I'm pretty sure this used to work.  I guess I will just have to rewrite this part.

Thank you.

Aaron TomoskyDirector of Solutions ConsultingCommented:
$query = "select column from table where price >= '".mysql_real_escape_string($low)."' and...

Sorry for not typing the whole thing out. I'm on my phone.
Schuyler KuhlAuthor Commented:
thanks very much.

Yes I see this is the proper way to do it now.

Thanks I appreciate your help greatly.

Best regards,

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.