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.
Counting entries in MySQL dB
Moderator: General Moderators
Re: Counting entries in MySQL dB
I've solved this:
It took some browsing my PHP books and some Google searching but I finally got there!
KD.
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>";
?>KD.
Re: Counting entries in MySQL dB
Thanks Tim. You got it in one!