如何查看多个SQL查询返回的行数

时间:2013-08-01 10:58:51

标签: php mysql

我有一个带有此代码的php脚本:

<?php
    $sql="SELECT * from reminders where duedate < DATE(now()) and dismissed = '' ";
    $rs=mysql_query($sql,$conn) or die(mysql_error());
    if(mysql_num_rows($rs) > 0)
    {
        ?>
        <div class="box">There are overdue reminders. <a href="/admin/reminders/reminders.php?action=overdue">Click Here to view</a></div>
        <script type="text/javascript">
        window.onload = function()
        {
            var answer = confirm("You have Overdue Reminders. Would you like to view them?")
            if (answer)
            {
                window.location = "/admin/reminders/reminders.phpaction=overdue";
            }
        }
        </script>
        <?php
    }
    $sql="SELECT * from reminders where duedate = DATE(now()) and dismissed = '' ";
    $rs=mysql_query($sql,$conn) or die(mysql_error());
    if(mysql_num_rows($rs) > 0)
    {
        ?>
        <div class="box">There are reminders due today. <a href="/admin/reminders/reminders.php?action=due">Click Here to view</a></div>
        <script type="text/javascript">
        window.onload = function()
        {
            var answer = confirm("You have Reminders Due today. Would you like to view them?")
            if (answer)
            {
                window.location = "/admin/reminders/reminders.php?action=due";
            }
        }
        </script>
        <?php
    }
    ?>
    <!-- list cstomers who've gone over their allocation -->
    <?php
    //sql to list all customers with support this month
    $startdate=date("Y-m-01 00:00:00");
    $enddate=date("Y-m-t 23:59:59");
    $sql="SELECT tickets.company, tickets.ticketnumber, SUM( TIMEDIFF( ticket_updates.timeend, ticket_updates.timestart ) ) /100 AS support_time_used
    FROM tickets, ticket_updates
    WHERE tickets.ticketnumber = ticket_updates.ticket_seq and tickets.datetime>'".$startdate."' and tickets.datetime<'".$enddate."' 
    GROUP BY tickets.company
    ORDER BY  tickets.company ASC";
    $rs=mysql_query($sql,$conn) or die (mysql_error());
    while($result=mysql_fetch_array($rs))
    {
        //loop through all support companies and if they have more than their allocated number of minutes, then show them up
        //first, get number of minutes in support allocation
        $sql="select company,support_minutes from customer where sequence = '".$result["company"]."' ";
        $rs2=mysql_query($sql,$conn) or die(mysql_error());
        $result2=mysql_fetch_array($rs2);
        if($result2["support_minutes"] != '0')
        {
            if ($result["support_time_used"]>$result2["support_minutes"])
            {
                $used_minutes = round($result["support_time_used"]);
                $used_hours = $used_minutes / 60;

                $total_minutes = round($result2["support_minutes"],0);
                $total_hours = $total_minutes / 60;
                //check whether ti display minutes or hours
                if($used_hours < 1)
                {
                    //customer has exceeded inclusive minutes/hours
                    //display minutes
                    echo '<div class="box"><strong><a href="/admin/customer/editcustomer.php?seq='.$result2["sequence"].'">'.$result2["company"].'</a> have used  '.$used_minutes.' minutes of their  '.$total_hours.' support hours this month</strong></div>';
                }
                elseif($used_hours > 1)
                {
                    //customer has exceeded inclusive minutes/hours
                    //display hours
                    echo '<div class="box"><strong><a href="/admin/customer/editcustomer.php?seq='.$result2["sequence"].'">'.$result2["company"].'</a> have used  '.$used_hours.' hours of their  '.$total_hours.' support hours this month</strong></div>';
                }
            }
        }
    }
    ?>
    <!-- END -->
    <!-- Display open service issues / status updates -->
    <?php
    $sql="SELECT * from servicestatus where status<>'Closed' ";
    $rs=mysql_query($sql,$conn) or die (mysql_error());
    $count = mysql_num_rows($rs);
    $result=mysql_fetch_array($rs);
    if(mysql_num_rows($rs) > 0)
    {
        echo '<div class="box"><strong>There are currently '.$count.' <a href="/admin/servicestatus/viewservicestatus.php?status=Open">Open Service Issues</a></strong></div>';
    }
    ?>
    <!-- END -->
    <!-- Display not viewed customer notes (added by call answering) -->
    <?php
    $sql="SELECT * from customer_notes where viewed = 'no' ";
    $rs=mysql_query($sql,$conn) or die (mysql_error());
    $count = mysql_num_rows($rs);
    if(mysql_num_rows($rs) > 0)
    {
        while($result=mysql_fetch_array($rs))
        {
            $sql2="SELECT * from customer where sequence = '".$result["customer"]."' ";
            $rs2=mysql_query($sql2,$conn) or die(mysql_error());
            $result2=mysql_fetch_array($rs2);

            echo '<div class="box"><strong>There are Customer Notes for '.$result2["company"].' that have\'t been viewed. <a href="javascript: void(0)" onclick="popup(\'/admin/customer/unviewed-notes.php?seq='.mysql_real_escape_string($result["sequence"]).'\')">Click Here</a></strong></div>';
        }
    }
    ?>

有很多SQL查询查询不同的表等,我知道你可以使用if(mysql_num_rows($rs) == 0) { echo 'something here'; },但这只能用于其中一个查询。什么是查看NONE查询是否返回任何行以显示消息的最佳方法?

2 个答案:

答案 0 :(得分:1)

在脚本开头,初始化变量:

$something_found = false;

然后在每个if(mysql_num_rows($rs) > 0)块内,执行:

$something_found = true;

然后在所有这些完成后测试$something_found

答案 1 :(得分:0)

您可以使用:

$sql="SELECT COUNT('id') as COUNT_ID,* from customer_notes where viewed = 'no' ";

//....

if($result['COUNT_ID'] > 0)
{
  ....
}