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
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
Share on other sites

  • 4 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.