Forum Moderators: coopster

Message Too Old, No Replies

Count query

         

dave1236

3:05 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Can someone provide some insight into how I can execute a count query and display the results in my page.

I want my query to count the number of comments related to a particular story, and display the results.

Duskrider

3:10 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Something like this?

$mystory = 'mystory'; // Whatever story you want to search

$query = "SELECT COUNT(comments) FROM mytable WHERE story = '$mystory'";
$result = mysql_query($query) or die("Could not get comments for $mystory");
$row = mysql_fetch_row($result);
$num = $row[0];

// Echo the count
echo "The count is $num";

Table names and such are examples of course.

[edited by: Duskrider at 3:12 pm (utc) on June 29, 2007]

henry0

5:05 pm on Jun 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or "fresh from the manual"
use mysql_num_rows [us2.php.net]

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

just add your WHERE CLAUSE

venelin13

8:38 pm on Jun 30, 2007 (gmt 0)

10+ Year Member




$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

It is not a good practice to use mysql_num_rows() to determinate the number of results. In your case, all the table1 content will be loaded into the memory and after that, the result records will be counted.

What about the table1 content is very large? Or just exceed the mysql memory limit? The database server will "freeze".