mysql query string in link using urlencode

Posted on 2011-04-23
Last Modified: 2013-12-12

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.

Question by:skykuhl
    LVL 38

    Accepted Solution

    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.

    Author Comment

    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.

    LVL 38

    Expert Comment

    by:Aaron Tomosky
    $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.

    Author Comment

    thanks very much.

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

    Thanks I appreciate your help greatly.

    Best regards,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now