Page 1 of 1

Count the number of entries

Posted: Sat Dec 10, 2005 8:03 pm
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

Posted: Sat Dec 10, 2005 8:11 pm
by Chris Corbyn
Use MySQL's count() function. You can also use mysql_num_rows() in PHP ;)

Posted: Sat Dec 10, 2005 8:14 pm
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 :)

Posted: Sat Dec 10, 2005 8:22 pm
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

Posted: Sat Dec 10, 2005 8:29 pm
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";

?>

Posted: Sun Dec 11, 2005 5:57 am
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

Posted: Sun Dec 11, 2005 7:19 am
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";

?>