dongmo6937 2013-08-01 10:58
浏览 204
已采纳

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

I have a php script with this code on it:

<?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>';
        }
    }
    ?>

there are many SQL Queries that query different tables etc, i know you can use if(mysql_num_rows($rs) == 0) { echo 'something here'; } but that would only be for one of the queries. whats the best way to see if NONE of the queries return any rows to display a message?

  • 写回答

2条回答 默认 最新

  • douzhangbao2187 2013-08-01 11:05
    关注

    At the beginning of the script, initialize a variable:

    $something_found = false;
    

    Then inside each of the if(mysql_num_rows($rs) > 0) blocks, do:

    $something_found = true;
    

    Then test $something_found after all of them are done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页