无法计算MySQL / PHP列中的值

时间:2013-05-24 14:53:35

标签: php mysql

我有下表:

ID  Name    Phone   Email   SchoolGymnasium City    Password    Status  
1                                                                 0

我想检查“Status”列中的整个“Directors”表中是否存在值0,如果是,则运行以下HTML代码:

<h1 class="page-title projects">Директори</h1>            
            <a href="javascript:;" id="add-project" class="btn add-project">Add Project</a>
            <div id="projects-alert" class="alert">
            <a href="#" class="close" title="Close">X</a>Quick tip: You can re-order projects by dragging and dropping them into place.</div>

否则运行以下代码:

<div id="no-projects">
        <h2>В момента няма подадени заявки.</h2>
        <p>Ако желаете може да добавите директор, като кликнете не бутон по-долу. Информация за вход в системата ще бъде изпратен на посоченият е-мейл.</p>
        <a href="javascript:;" class="btn add-project">Добавете Директор</a>
    </div>

要做到这一点,我已经做了以下事情,并没有发生:

<?php $jojo = mysql_query("SELECT COUNT(id) AS Status FROM Directors WHERE Status = '0'");
            if(mysql_num_rows($jojo) > 0){?>
            <h1 class="page-title projects">Директори</h1>            
            <a href="http://denismm778.dunked.com/admin/projects/new" id="add-project" class="btn add-project">Add Project</a>
            <div id="projects-alert" class="alert">
            <a href="#" class="close" title="Close">X</a>Quick tip: You can re-order projects by dragging and dropping them into place.</div>
    <?php } else{ ?>
<div id="no-projects">
        <h2>В момента няма подадени заявки.</h2>
        <p>Ако желаете може да добавите директор, като кликнете не бутон по-долу. Информация за вход в системата ще бъде изпратен на посоченият е-мейл.</p>
        <a href="javascript:;" class="btn add-project">Добавете Директор</a>
    </div><?php }?> 

这个想法是这样的。如果Status列中的Status 0显示第一个HTML代码,则显示第二个代码。

3 个答案:

答案 0 :(得分:1)

您的查询将始终返回一行。您需要检查该行中“状态”字段的值,以查看IT是否> 0。

答案 1 :(得分:1)

使用其他名称进行计数。 试试这种方式:

$query = "SELECT COUNT(id) AS count FROM Directors WHERE Status = '0'";
$results = mysql_query($query);
$values = mysql_fetch_assoc($results);
$num_rows = $values['count'];
if($num_rows > 0)
    ....

答案 2 :(得分:0)

在您的查询中:

SELECT COUNT(id) AS Status FROM Directors WHERE Status = '0'

您正在检查返回的行数是否大于0 但是您的查询将始终返回一行 即包含状态为0的行数的行。因此,您的条件始终为真

所以而不是

if(mysql_num_rows($jojo) > 0)

使用

$result = mysql_fetch_array($jojo);

if($result['Status'] > 0){

//display html

}

else{

//display alternate html

}