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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question how to count records on php

    PHP Code:
    //DISPLAYING CONTENT!!
    echo "----------display content <br>";
    $select mysql_query("SELECT * FROM guest_info where room_type = 'single room' and
    check_in >= '0000-00-00'"
    ); 

    while(
    $row mysql_fetch_array($select)){

    echo 
    $row['guest_id'] . "<br>" "<br>";

    }
    //COUNTING CONTENT 
    echo "----------counting <br>";


    $total_results mysql_query("SELECT count('guest_id') FROM guest_info where room_type = 'single room' and check_in >= '0000-00-00'");
                                  
    $row mysql_fetch_array($total_results);

    $result count($row);

    echo 
    $result

    here is my code.. the displaying part is doing fine.. but its not counting

    anybody can help me? thanks

  • #2
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    I don't know much about the 'count' in a mysql query - but why not just do this:

    PHP Code:
    $total_results mysql_query("SELECT 'guest_id' FROM guest_info where room_type = 'single room' and check_in >= '0000-00-00'");

    $result mysql_num_rows($total_results);

    echo 
    $result

  • #3
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    Actually - the other solution is:

    PHP Code:
    //DISPLAYING CONTENT!!
    echo "----------display content <br>";
    $select mysql_query("SELECT * FROM guest_info where room_type = 'single room' and
    check_in >= '0000-00-00'"
    ); 

    while(
    $row mysql_fetch_array($select)){
    $count++;
    echo 
    $row['guest_id'] . "<br>" "<br>";


    //COUNTING CONTENT 
    echo "----------counting <br>";
    echo 
    $count

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    counting is best done by the DB itself:`
    Code:
    SELECT COUNT(*) AS `number` FROM …
    recommendation, don’t use * but the field names you actually need. this way you get the array keys from looking at the sole query string.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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