Count the number of entries
Posted: Sat Dec 10, 2005 8:03 pm
hi again. i am wanting to display the number of entries in database where the entry in blogID = $blogid
Hope that makes sence
Eric
Hope that makes sence
Eric
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
okeys then ill have a serch and playd11wtq wrote:Use MySQL's count() function. You can also use mysql_num_rows() in PHP
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";
?>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...ericburnard wrote:aaahhhhhhh ive worked it out now.
Heres what i came up with it if anyone wants to know
EricCode: 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"; ?>
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";
?>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";
?>