如何从表中显示状态

时间:2014-07-26 10:05:58

标签: php mysql

我有一张表消息

id | name   |  email         |  message |  status
-------------------------------------------------
1  | name1  |  n1@gmail.com  |  hello1  |  unread
-------------------------------------------------
3  | name2  |  n2@gmail.com  |  hello2  |  unread
-------------------------------------------------
7  | name1  |  n1@gmail.com  |  hello3  |  read
-------------------------------------------------
8  | name1  |  n1@gmail.com  |  hello4  |  read
-------------------------------------------------
9  | name2  |  n2@gmail.com  |  hello5  |  read
-------------------------------------------------

我想根据电子邮件显示状态。

例如:对于电子邮件n1@gmail.com

有1条未读和2条已读信息。所以我想为电子邮件n1@gmail.com

显示status = unread(1)

我的输出应该是:

email         |  status
--------------------------
n1@gmail.com  |  unread(1)
--------------------------

我的尝试是:

<?php
include"connection.php";
extract($_SESSION);
extract($_REQUEST);
$q2=mysql_query("select DISTINCT(email) from message where email='$email'") or die(mysql_error());
while($r2=mysql_fetch_array($q2))
{
    extract($r2);
    $s1=mysql_query("select status from message where email='$email'");
    $no=mysql_num_rows($s1);
    echo $s1['status'];
    echo "unread("; 
    if($no['status']=='read') 
    { 
    echo "0"; 
    } 
    else 
    { 
    echo $no; 
    } 
    echo ") ";
}

?>

但是它给出了总行数。怎么可能?请帮忙!

1 个答案:

答案 0 :(得分:0)

$s1=mysql_query("select status from message where email='$email'");

它总是会给你总行数,因为你提取的总消息状态没有条件,你可以试试这个

$s1=mysql_query("select status from message where email='$email' AND status='unread'");
$no=mysql_num_rows($s1);
if($no==0)
{
   $msg = 0;
}
else
{
    $msg=$no;
}

然后你可以输出这个

echo $msg;