SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How Can I Count The Contents of a Table?

    I have scoured through hotscripts.com and PHP Resource Index, to try and find a simple count script, but to no avail.

    I would like to show how many churches are stored within my database, and being new to PHP, I do not know how to.

    I can create the connection and selecting the table, but do not know what goes after that.

    Here is how I would do the first bit - if you could add any code of yours to it, then I would be most thankful.

    ...Or if it could be created in it's own file, so I could just use an "include" file in the other pages, then it would be better - but I'm not too sure about this.


    Thanks in Advance
    Ian Gunter

    PHP Code:
    <?php

    // create connection
    $connection mysql_connect("localhost","thedesi_aog","********")
        or die(
    "Couldn't make connection.");

    // select database
    $db mysql_select_db("thedesi_aogeastern"$connection)
        or die(
    "Couldn't select database.");

    // this is where i need your code

    // echo out the results
    echo "<tr>
        <td valign=\"top\"><p class="
    small">churches in database : $amount_of_churches</td>
    </tr>"
    ;
    }

    echo 
    "</table>

    </body>
    </html>"
    ;

    // free resources and close connection
    mysql_free_result($sql_result);
    mysql_close($connection);

    ?>

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How Can I Count The Contents of a Table?

    PHP Code:
    $result=mysql_query("SELECT count(*) AS count from your_table");
    $amount_of_churches=mysql_num_rows($result); 
    or:
    PHP Code:
    $result=mysql_query("SELECT count(*) AS count from your_table");
    $amount_of_churches=mysql_num_rows($result);
    $row=mysql_fetch_array($result);
    $amount_of_churches=$row['count']; 
    that's assuming each row in your database corresponds to a church...

  3. #3
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that Redemption

    The second example worked for me, whereas the first one just told me there was "1" church when there was more.

    Out of curiosity, do you know why this was, and should've it worked?


    Ian Gunter

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second one places the resultset in an array where as the first one continually replaces the same resultset with the new query results thus having a maximum of one result per query.

    I have no idea if that made any sense at all.

    Sketch
    Aaron Brazell
    Technosailor



  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by spoondevil
    The second example worked for me, whereas the first one just told me there was "1" church when there was more.
    because "SELECT COUNT(*) FROM table" always returns 1 row, whether there's 0 rows in the table or 1,000,000. the mysql_num_rows() function shouldn't be in either one. i'd do it like this:

    PHP Code:
    $result mysql_query("SELECT COUNT(*) FROM table");
    $churches mysql_result($result0); 
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR
    [B]the mysql_num_rows() function shouldn't be in either one.
    you're right there... that's for my laziness... i just copied the first example over and forgot to remove the mysql_num_rows() line

    sorry bout that


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
  •