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

  • Home
  • SEARCH
  • 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 7879197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:50:56+00:00 2026-06-03T03:50:56+00:00

I was using this method to get row-counts for SELECT statements, but it didn’t

  • 0

I was using this method to get row-counts for SELECT statements, but it didn’t work correctly for LIMIT statements, so I added code to remove the LIMIT from the sql string, but now it doesn’t work at all!

When I started using queries with a LIMIT X, X at the end, I had to add the section at the top to strip that part from the query, because this method only returns one column – count – but the LIMIT offset would cause it to return nothing.

function dbRowsCount($sql, $data) {

    # removes "LIMIT X,X" from query
    $no_limit_sql = $sql;
    $pos = stripos($sql, " LIMIT");
    if ($pos!==false) {
        $no_limit_sql = substr($sql, 0, $pos).";";
        # logErrors("original sql=".$sql);
        # logErrors("pos=".$pos."<br>new sql=".$no_limit_sql);
    }

    # query SELECT COUNT(*) instead of SELECT...
    # returns row-count
    $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/is';     
    if (preg_match($regex, $no_limit_sql, $output) > 0) {
        dbQuery("SELECT COUNT(*) AS count FROM {$output[1]}", $data);
        $result = dbFetch();
        $last_rowcount = (int)$result["count"];
        if ($pos!==false) {
            # logErrors("after regex="."SELECT COUNT(*) AS count FROM {$output[1]}");
            # logErrors("row count=".$last_rowcount);
        }
        return $last_rowcount;
    } else {
        # logErrors("Oops! There was an error:<br>Regex did not match SQL.");
        return -1;
    }
}

This put directly into MySQL gives count=8:

SELECT COUNT(*) AS count FROM tbl_product WHERE 1 ORDER BY last_update DESC;

The output from the dbRowsCount($sql, $data) method shows this query should end up the same:

original sql=SELECT * FROM tbl_product WHERE 1 ORDER BY last_update DESC LIMIT ?, ?;
pos=59
new sql=SELECT * FROM tbl_product WHERE 1 ORDER BY last_update DESC;
after regex=SELECT COUNT(*) AS count FROM tbl_product WHERE 1 ORDER BY last_update DESC;
row count=0

It was working before, it was returning rows=8 without the part I added for removing “LIMIT X,X” from the query, but only when the LIMIT was 0,X. When I tried LIMIT 5,5 it wouldn’t fetch the column because of the offset, and now it’s returning 0 rows for any values.

I also tried this directly with PDO, it worked correctly for both int and string parameters! But using the PDOStatement->rowCount(). (It’s not recommended for SELECT queries, so I used the above dbRowsCount method).

function dbDebugTest() {
    global $db;
    # logErrors("Beginning dbDebugTest()");
    $stmt = $db->prepare("SELECT * FROM tbl_product WHERE 1 ORDER BY last_update DESC LIMIT ?,?");
    $offset = "5";
    $display = "5";
    $stmt->execute(array($offset,$display));
    # logErrors("dbDebugTest() returned rows: ".$stmt->rowCount());
}

What is causing the first method to return 0 rows?

  • 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-03T03:50:57+00:00Added an answer on June 3, 2026 at 3:50 am

    All I can see is that when a LIMIT is present you add a semi colon behind the query. Perhaps that’s the cause of your problem.

    If that’s not the problem, assuming dbQuery and dbFetch are working correctly and didn’t change, your table is empty? 🙂

    OP Answer I was still forwarding the $data array to the query when there are no params anymore (since I removed LIMIT X, X)

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

Sidebar

Related Questions

To get all contacts I'm using ABAddressBookCopyArrayOfAllPeople method but, this method return all contacts
I am using this method to get a list of week start from my
I'm using this method to install an application, but it doesn't put the applications
I'm creating new Site Definitions using this method: http://weblogs.asp.net/paulballard/archive/2007/04/09/creating-a-custom-sharepoint-2007-portal-site-definition-using-the-portalprovisioningprovider-class.aspx and when they get created,
when using this method public List<Field> getFieldWithoutId(List<Integer> idSections) throws Exception { try { Connection
I deployed an application using this method and it worked very good. However, there
I find myself using this method a ton to perform actions based on a
I'm currently using this method in calling batch files in vb.net: Private Sub Button3_Click(ByVal
Should I always be using this method when rendering? Does it slow down much
I am using this method - (void)keyboardWillShow:(NSNotification *)notification that is triggered when the keyboard

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.