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
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 :)
Link to comment
Share on other sites

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

[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]
Link to comment
Share on other sites

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.