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 8355451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:51:58+00:00 2026-06-09T09:51:58+00:00

I got a few queries built dynamically by my scripts. They usually fit the

  • 0

I got a few queries built dynamically by my scripts. They usually fit the following template:

SELECT ... 
FROM ... 
JOIN ... 
WHERE ... (lots of filters, search conditions, etc. here) ... 
ORDER BY (optional) ... 
LIMIT (optional) ... OFFSET (optional)

I want to remove the LIMIT and OFFSET parts from the query. I used

$sql_entitati = implode("LIMIT", explode("LIMIT", $sql_entitati, -1));

to do it but then it hit me: what if there’s no LIMIT in the query and what if the only LIMIT is somewhere in the where clauses?

So my question to you is: How can I safely remove everything after the LIMIT key word, without screwing it up if there’s no LIMIT and/or there’s a “LIMIT” somewhere in the where clause? All this done in php.

A bit of an edit for clarity:

the algorithm i use:

$sql = implode("LIMIT", explode("LIMIT", $sql, -1));

Will work on 99% of the cases. The problem occurs when the “LIMIT” key word at the end is missing, AND there is “LIMIT” written somewhere in the conditions. for example:

 SELECT * FROM table WHERE bla = 'SPEED LIMIT' ORDER BY table.a

this is the problem i need to tackle.

Solved using the following algorithm (Credit to techfoobar):

    $p = strrpos($sql, "LIMIT");
    if($p !== false) {
        $q = strpos($sql, ")", $p);
        $r = strpos($sql, "'", $p);
        $s = strpos($sql, "\"", $p);
        if($q === false && $r === false && $s === false)
            $sql = substr($sql, 0, $p);
    }
  • 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-06-09T09:51:59+00:00Added an answer on June 9, 2026 at 9:51 am

    You should do something like:

    1. Get the position of the last “LIMIT” – store this position in say, p
    2. Ensure that you do not have a ")" character after p – if so, it is part of some inner query inside a condition etc..
    3. Ensure that you do not have a "'" character after p – if so, it is part of some user input string
    4. If steps 2 and 3 are passed, strip off everything after p

    More hints:

    • For step 1 – use strrpos for the last occurrence of LIMIT
    • For steps 2 and 3, use strpos with p as the search start offset
    • For step4, use substr to strip off everything after p
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a few queries (20+) which all return the following three columns: Building
I have got a few tables which I am trying to join. I just
I've got few doubts regarding quoting mysql queries in Zend framework. Though this question
I've got a page that contains a fair few database queries, each appended with
I've got experience with MySQL, DB2, and Oracle, but aside from writing a few
I've got few points and I wanted to draw them, and join them with
I got a few examples from TTR documentation like: data(ttrc) mfi <- MFI(ttrc[,c(High,Low,Close)], ttrc[,Volume])
i have form in drupal which uploads images and has got few checkboxes in
So I have googled whole day, got few answers how to do this, and
I got a few WCFs that are running on an intranet on IIS 7.

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.