Counting entries in MySQL dB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Counting entries in MySQL dB

Post by kdidymus »

For those of you who have been following my progress and perhaps helping with my seemingly endless stream of silly questions, you'll know that I'm creating a family tree website using PHP and MySQL. The site can be seen at http://www.didymus.org.uk

What I want to do is create a PHP page which will display by default showing the number of ancestors in my database.

So what I need is some help in coding a page of PHP which will connect to my database, count the number of rows and set this to the string $entries. I will then echo this string.

Any ideas how I can go about this?

Many thanks.
timsewell
Forum Newbie
Posts: 21
Joined: Tue Feb 26, 2008 5:43 am
Location: Hove, England

Re: Counting entries in MySQL dB

Post by timsewell »

kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Counting entries in MySQL dB

Post by kdidymus »

I've solved this:

Code: Select all

<?php
/* Program: count.php
 * Desc:    Counts rows and returns statistics.
 */
  // CONNECT TO MYSQL AND COUNT ROWS
  include_once("*******.inc.php");
  $cxn = mysql_connect($host,$user,$password)
         or die ("couldn't connect to server");
  mysql_select_db($database);
  $result = mysql_query("select * FROM tree") or exit(mysql_error());
  $num_rows = mysql_num_rows($result);
  $number = $num_rows;
  $total = 1189;
  $remain = $total - $number;
  echo "<html><head><title>Welcome!</title></head>";
  echo "$number of $total completed. $remain to go!";
  echo "</body></html>";
?>
It took some browsing my PHP books and some Google searching but I finally got there!

KD.
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Counting entries in MySQL dB

Post by kdidymus »

Thanks Tim. You got it in one!
Post Reply