Jump to content

Calculate number of rows


Gibbs

Recommended Posts

Hello,

I have a database named "mail" which has 5 different fields. These are the fields:

UserTo, UserFrom, Subject, Message, status, SentDate and mail_id.

What I want to do is get the total number of rows that have status set to "unread" in them for a certain username. I would like to have a variable with a number so users know how many unread mails they have.

Any help would be great.

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/34485-calculate-number-of-rows/
Share on other sites

[code]$get_mail = mysql_query("SELECT COUNT('status') FROM $tblnamer AS TOTAL WHERE UserTo='$justr' ORDER BY SentDate") or die ("<br><br>Can't execute $sql: " .mysql_error());[/code]

This works (thanks to your help). When I run it through PHPMyAdmin is gives me the number 2.

When I run $inboxcount = mysql_num_rows($get_mail); through PHP I get the number 1.

Number 2 is correct. How can I get that number into a PHP string?

Thanks by the way :)
Actually I do have a problem with this (dont want to create another thread).

I need to read only rows that have "status" set to unread. I've tried this but it doesn't work....
[code]SELECT COUNT(status='unread') FROM $tblnamer AS TOTAL WHERE UserTo='$justr' ORDER BY SentDate[/code]

Ah figured it out. SORRY! :P
[code] SELECT COUNT('status') FROM $tblnamer AS TOTAL WHERE UserTo='$justr' AND `status` = 'unread' ORDER BY SentDate[/code]

the reason mysql_num_rows is only returning "1" is because you're running it against the SELECT COUNT() query (which is only returning one row, with one record that has the count in it). mysql_num_rows is supposed to be used with your real query. so...

[code]<?php
$result=mysql_query("SELECT * FROM `$tblnamer` WHERE `UserTo` = '$justr' ORDER BY `SentDate`");
$total_rows=mysql_num_rows($result);
while(mysql_fetch_assoc($result)) {
... blah blah
[/code]

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.