SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Location
    Kansas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PDO equivalent of mysql_num_rows() ?

    I am trying to figure out how I can get the number of rows returned by a fetch. Is there an equivalent of mysql_num_rows() that comes with PDO? I noticed PDOStatement->rowCount(), however; on select statements this is not a guaranteed way of getting back the number of rows returned by the statement as this behavior is not guaranteed for all databases.

    Anyone have any ideas on how to go about getting the number of rows?

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can pretty much consider PDOStatement->rowCount() the PDO equivalent of mysql_num_rows() because both are guaranteed to work with MySQL. However, if what you are looking for is a database independent API, you cannot rely on PDOStatement->rowCount() and certainly not on mysql_num_rows() for obvious reasons.

    Using SELECT COUNT(*) statement will serve your purpose.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    AFAIK, you have to count the rows in the array.
    PHP Code:
    echo count$stmt->fetchAllPDO::FETCH_BOTH ) ); 
    Maybe there is a better way?

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •