Jump to content

count data in mysql


shirvo

Recommended Posts

For all of the messages:
[code]SELECT * FROM table WHERE id = '$id' //In this case, $id is 1[/code]

For all the ones that have been read:
[code]SELECT * FROM table WHERE id = '$id' AND read = 'true' //Or whatever the value to show a message has been stored as true.[/code]

You can then do this to output the number:
[code]<?php
$query = "mysql_query(SELECT * FROM table WHERE id = '$id' AND read = 'true')";
$numrows = "mysql_num_rows($query)"
echo $numrows." messages have been read."
?>[/code]

Hope this is what you want.
Link to comment
https://forums.phpfreaks.com/topic/30493-count-data-in-mysql/#findComment-140367
Share on other sites

If you are only wanting the number of records, and not the data itself, use a count query.

[code]SELECT COUNT(*) WHERE WHERE id = '$id' AND read = 'true'[/code]

Then use mysql_result to get your answer:

[code]$result = mysql_query("SELECT COUNT(*) WHERE WHERE id = '$id' AND read = 'true'");
$count = mysql_result($result, 0);[/code]

The reason for doing this is because if you use Cagecrawler's method you are actually retrieving the data and storing it in a variable.  If there is a large amount of data retrieved by the query, it will take up a large amount of memory as well.  Additionally, if your web server and database server are different machines, that data will have to traverse the network, even if you aren't going to use it for anything other than a "mysql_num_rows" function.
Link to comment
https://forums.phpfreaks.com/topic/30493-count-data-in-mysql/#findComment-140442
Share on other sites

  • 4 years later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.