#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    1
    Rep Power
    0
    How can I find out the number of rows in the result set from a Select statement?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    $mysql_link=mysql_connect("hostname", "username", "password");
    mysql_select_db("DB_NAME");
    $query="select * from table";
    $result=mysql_query($query, $mysql_link);
    $affected_rows=mysql_affected_rows($mysql_link);
    .
    .
    .
    // $affected_rows contains the number of rows returned from the query
  4. #3
  5. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Actually, affected_rows is only valid after an update.

    Here you want:

    $num_rows=mysql_num_rows($result);
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    15
    Uh, no....mysql_affected_rows() works after a select statement, too.
  8. #5
  9. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    From the PHP manual:

    -----------------------------------

    mysql_affected_rows

    mysql_affected_rows -- Get number of affected rows in previous MySQL operation
    Description

    int mysql_affected_rows(int [link_identifier] );

    mysql_affected_rows() returns the number of rows affected by the last INSERT, UPDATE or DELETE query on the server associated with the specified link identifier. If the link identifier isn't specified, the last opened link is assumed.

    If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero.

    This command is not effective for SELECT statements, only on statements which modify records. To retrieve the number of rows returned from a SELECT, use mysql_num_rows().

    ---------------------------------


    Note the terminology: "not effective for SELECT statements...".

    It WILL work in some instances depending on the query and the versions of PHP and MySQL involved, BUT is UNRELIABLE and should not be used.

Similar Threads

  1. Replies: 1
    Last Post: January 15th, 2004, 08:00 AM
  2. Replies: 5
    Last Post: January 11th, 2004, 08:19 PM
  3. How to select 10 highest rows
    By Blubman in forum MySQL Help
    Replies: 2
    Last Post: January 5th, 2004, 10:25 AM
  4. Mysql update: 0 rows Select: 1 row ?
    By Cyclefreak in forum MySQL Help
    Replies: 2
    Last Post: January 3rd, 2004, 10:06 AM
  5. Replies: 1
    Last Post: May 18th, 2003, 07:06 AM

IMN logo majestic logo threadwatch logo seochat tools logo