Count the number of entries

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
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Count the number of entries

Post by ericburnard »

hi again. i am wanting to display the number of entries in database where the entry in blogID = $blogid

Hope that makes sence

Eric
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Use MySQL's count() function. You can also use mysql_num_rows() in PHP ;)
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

d11wtq wrote:Use MySQL's count() function. You can also use mysql_num_rows() in PHP ;)
okeys then ill have a serch and play :)
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

aaahhhhhhh ive worked it out now.

Heres what i came up with it if anyone wants to know

Code: Select all

<?php
// Connecting, selecting database
mysql_connect('localhost', '*********', '*************')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('madashat_blog') or die('Could not select database');

$blogid = $_GET['blogid'];

$query = "SELECT * FROM comments where blogID = '$blogid'"; 
$result = mysql_query($query) or die(mysql_error().'<br />'.$query); 

$number_of_rows = mysql_num_rows($result); 

echo "$number_of_rows";

?>
Eric
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ericburnard wrote:aaahhhhhhh ive worked it out now.

Heres what i came up with it if anyone wants to know

Code: Select all

<?php
// Connecting, selecting database
mysql_connect('localhost', '*********', '*************')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('madashat_blog') or die('Could not select database');

$blogid = $_GET['blogid'];

$query = "SELECT * FROM comments where blogID = '$blogid'"; 
$result = mysql_query($query) or die(mysql_error().'<br />'.$query); 

$number_of_rows = mysql_num_rows($result); 

echo "$number_of_rows";

?>
Eric
If you're looking for a speed increase use MySQL as much as possible. No need to use PHP when it can be done in the query...

Code: Select all

<?php
// Connecting, selecting database
mysql_connect('localhost', '*********', '*************')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('madashat_blog') or die('Could not select database');

$blogid = $_GET['blogid'];

$query = "SELECT count(*) FROM comments where blogID = '$blogid'"; 
$result = mysql_query($query) or die(mysql_error().'<br />'.$query); 

$number_of_rows = mysql_result($result, 0, 0); 

echo "$number_of_rows";

?>
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

ok i have tried that but it always gives me a value of 1 and not 0 when there are no entries present

eric
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

It's done this way.

Code: Select all

<?php
// Connecting, selecting database
mysql_connect('localhost', '*********', '*************')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('madashat_blog') or die('Could not select database');

$blogid = $_GET['blogid'];

$query = "SELECT count(*) AS total FROM comments where blogID = '$blogid'"; 
$result = mysql_query($query) or die(mysql_error().'<br />'.$query); 
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$number_of_rows = $row['total'];

echo "$number_of_rows";

?>
Post Reply