duanhan5230 2018-07-19 13:08
浏览 81
已采纳

当PHP中的启动时间大于1小时时,GET为空

I have a table which calculates the cost based on time.I need to display table based on time with 1 hour limit. If time is greater than 1 hour it should display null in the cost column.How can I do that.

dashboard.php

        <?php
                    $host    = "localhost";
                    $user    = "root";
                    $pass    = "";
                    $db_name = "test2";
                    $lastId="";


                     $date = strtotime('now') - 3600;  
                     print_r($date);  
                     $week = strtotime('now') - 604800;  
                     $month = strtotime('now') - 2592000 ;        

                    //create connection
                    $con = mysqli_connect($host, $user, $pass, $db_name);

                    //test if connection failed
                    $result = mysqli_query($con,"SELECT SUM(cost) FROM track_data WHERE start_time > 
                      DATE_SUB(NOW(), INTERVAL 1 HOUR) ");

                    $result1 =  mysqli_query($con,"SELECT SUM(cost) FROM track_data WHERE start_time > 
                      DATE_SUB(NOW(), INTERVAL 1 WEEK)");

                    $result2 =  mysqli_query($con,"SELECT SUM(cost) FROM track_data WHERE start_time > 
                      DATE_SUB(NOW(), INTERVAL 1 MONTH)");


                    ?>

    <div class="col-md-6 col-md-offset-3">
            <table class="table table-hover table-striped">    
            <tr class="t_head"> 
              <th>  Days </th>
              <th> Usage  </th>
              <th> Total Cost  </th>

            </tr>       
    <?php


          while($row = mysqli_fetch_array($result))


    {



      echo "<tr id='emp_salary'>"; 

     echo "<td> Today </td>";
      echo "<td>1 Hour</td>";
    echo "<td>" .$row[0]. "</td>";

    echo "</tr>";
    }
?>
</table>
</div>

enter image description here

As u can see the above image the total cost column is empty. Instead of empty value I should get NUll or Zero.

  • 写回答

1条回答 默认 最新

  • douluoqiu4538 2018-07-19 13:32
    关注

    You should use COALESCE

    Because if your sql query returns no rows or the sum of rows is null then your echo will not actually echo anything.

    Try this

    $result = mysqli_query($con,"SELECT COALESCE(SUM(cost),0) FROM track_data 
    WHERE start_time > DATE_SUB(NOW(), INTERVAL 1 HOUR) ");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。