Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:49:26+00:00 2026-05-23T15:49:26+00:00

Basically, what I want to do is this: mysql_query(SELECT … FROM … ORDER BY

  • 0

Basically, what I want to do is this:

mysql_query("SELECT ... FROM ... ORDER BY $_GET[order]")

They can obviously easily create a SQL error by putting non-sense in there, but mysql_query only allows you to execute 1 query, so they can’t put something like 1; DROP TABLE ....

Is there any damage a malicious user could do, other than creating a syntax error?

If so, how can I sanitize the query?

There’s a lot of logic built on the $_GET['order'] variable being in SQL-like syntax, so I really don’t want to change the format.


To clarify, $_GET['order'] won’t just be a single field/column. It might be something like last_name DESC, first_name ASC.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:49:27+00:00Added an answer on May 23, 2026 at 3:49 pm

    Yes, SQL injection attacks can use an unescaped ORDER BY clause as a vector. There’s an explanation of how this can be exploited and how to avoid this problem here:

    http://josephkeeler.com/2009/05/php-security-sql-injection-in-order-by/

    That blog post recommends using a white list to validate the ORDER BY parameter against, which is almost certainly the safest approach.


    To respond to the update, even if the clause is complex, you can still write a routine that validates it against a whitelist, for example:

    function validate_order_by($order_by_parameter) {
        $columns = array('first_name', 'last_name', 'zip', 'created_at');
    
        $parts = preg_split("/[\s,]+/", $order_by_parameter);
    
        foreach ($parts as $part) {
            $subparts = preg_split("/\s+/", $part);
    
            if (count($subparts) < 0 || count($subparts) > 2) {
               // Too many or too few parts.
               return false;
            }
    
            if (!in_array($subparts[0], $columns)) {
               // Column name is invalid.
               return false;
            }
    
            if (count($subparts) == 2 
                && !in_array(strtoupper($subparts[1]), array('ASC', 'DESC')) {
              // ASC or DESC is invalid
              return false;
            }
        }
    
        return true;
    }
    

    Even if the ORDER BY clause is complex, it’s still made only out of values you supply (assuming you’re not letting users edit it by hand). You can still validate using a white list.

    I should also add that I normally don’t like to expose my database structure in URLs or other places in the UI and will often alias the stuff in the parameters in the URLs and map it to the real values using a hash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my query: $result = mysql_query(SELECT * FROM Posts WHERE MATCH (City) AGAINST
I want to create a MySQL Query similar to SELECT city, state, country FROM
mysql_query(SELECT * FROM foo WHERE id ='$foo' OR id = '$foo2); This doesn't work.
Basically I have a mysql query something like the following: mysql_query(SELECT n.title, v.value FROM
I basically want to do this: grep 'example.com' www_log > example.com.YYYY-MM-DD-H:i:S.log ...with of course
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
Basically I want to do this: public interface A { void a(); } public
Basically i want to do this. aa causes a bad cast exception. NOTE: o
This is an incredibly simple question (I'm new to Python). I basically want a
Basically I want to be able to do this: session.ExecuteSql(...); I don't need it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.