Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP count() returning 1 on mysql result set, even when empty!

    I'm at a complete loss. How do I count the number of results a sql call returned, because count() sure isn't doing it.

    Anyone know what's going on?

    Thanks!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Please post your code. Are you executing a count() function in your query?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    $query = ("SELECT * FROM `table_name` WHERE name='johnDoe'");
    $amountOfJohnDoes mysql_num_rows($query); 

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    SELECT * FROM `table_name` WHERE name='johnDoe'";
    No. That's very inefficient for that purpose! Use something like,
    PHP Code:
    $result =mysql_query ("SELECT count(*) as count FROM `table_name` WHERE name='johnDoe'");
    echo 
    $value=mysql_result($result,0); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Quote Originally Posted by abduraooft View Post
    No. That's very inefficient for that purpose! Use something like,
    PHP Code:
    $result =mysql_query ("SELECT count(*) as count FROM `table_name` WHERE name='johnDoe'");
    echo 
    $value=mysql_result($result,0); 
    I think Phil Jackson simply meant using mysql_num_rows() to get the number of rows returned. The OP did not ask how to get number of all records in a table. It seems he needed the number of rows in the returned recordset. For which mysql_num_rows() is really the best solution.

  • #6
    New Coder
    Join Date
    Apr 2004
    Location
    Fsd / PK
    Posts
    68
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Phil Jackson View Post
    PHP Code:
    $query = ("SELECT * FROM `table_name` WHERE name='johnDoe'");
    $amountOfJohnDoes mysql_num_rows($query); 
    before using the mysql_num_rows, you need to execute the query to get the record set. then count the number of records in the record set.

  • #7
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Quote Originally Posted by Ashif View Post
    before using the mysql_num_rows, you need to execute the query to get the record set. then count the number of records in the record set.
    Yes just to clarify that would mean:

    PHP Code:
    //Query Txt
    $query "SELECT * FROM `table_name` WHERE name='johnDoe'";

    //Actually Execute The Query, and store the result set in $result
    $result mysql_query($query) or die(mysql_error());

    //Now you can perform num_rows on the result resource :)
    $amountOfJohnDoes mysql_num_rows($result); 


  •  

    Posting Permissions

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