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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:45:24+00:00 2026-05-21T05:45:24+00:00

I have just changed my database connection. I am not used to the PDO

  • 0

I have just changed my database connection. I am not used to the PDO class or OOP yet. Anyway, I connect to the db like this:

        $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
        try
        {
            $this->db = new PDO($dsn, DB_USER, DB_PASS);
        }
        catch ( Exception $e )
        {
            die ( $e->getMessage() );
        }

I am trying to get number of rows from this query:

    $ip = $this->ip(); 
    $sql = "SELECT `id` FROM `login_failed`
            WHERE `ip` = :ip AND `time` BETWEEN NOW( ) - INTERVAL 120 MINUTE AND NOW( )
            LIMIT 3";
    try 
    {
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':ip', $ip, PDO::PARAM_STR);
        $result = $stmt->execute(); // $result = true
        $n = $stmt->num_rows ; // n = NULL?
        $stmt->closeCursor(); 
    }       
    catch (Exception $e)
    {
        die ($e->getMessage() ); 
    }

In phpmyadmin I get a result so my query is correct, but $n is NULL for some reason.. How do I get number of rows with PDO

  • 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-21T05:45:24+00:00Added an answer on May 21, 2026 at 5:45 am

    $stmt is of type PDOStatement. That class has no num_rows property.

    You might be looking for rowCount instead, but the documentation for that states:

    If the last SQL statement executed by
    the associated PDOStatement was a
    SELECT statement, some databases may
    return the number of rows returned by
    that statement. However, this
    behaviour is not guaranteed for all
    databases and should not be relied on
    for portable applications.

    The long and the short if it is that, if you want to actually SELECT all that data, you can reliably determine how many rows were returned by iterating over the result set (or just call fetchAll and count the items in the array). If you don’t need the data but just a number, use SELECT COUNT instead.

    So, to count the rows without changing the query:

    $result = $stmt->execute();
    $rows = $stmt->fetchAll(); // assuming $result == true
    $n = count($rows);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used EF to generate classes for my database tables: public partial class
I have just installed a new ssl certificate on GlassFish 3. I also changed
I have a SQLite database with with just over 6,000 rows of addresses in
There is probably a simple answer to this question but I do not have
Possible References I have found a similar problem: Refreshing a SQL database connection which
I have changed my previous code so I am not using 'using'. It work
This is very simple linq and I am just beginning with it. I have
How can we change body ID while page loading? I have just developed an
I've just create a new MVC project, and have made no changes at all,
Have just started converting an existing job tracking system into an ASP.NET MVC application.

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.