处理mysqli返回的null值

huangapple go评论87阅读模式
英文:

handling mysqli returned null value

问题

我正在编写一个小型的网络应用程序作为报告工具。我已经编写了一个循环来获取每天的值,但在某一天,我们的服务器出了问题,数据丢失了。当我在MySQL中运行查询时,返回了NULL。我正在尝试编写一些错误处理,但它似乎无法正常工作。我做错了什么?

  1. $ath_q = "SELECT SUM(performances*(TIME_TO_SEC(duration)/3600)) AS 'ATH'
  2. from streamstats.livestats INNER JOIN streamstats.library USING(artist,song) where ts BETWEEN ? AND ?";
  3. $i = 0;
  4. $dailyATH = [];
  5. echo count($dateRange)."<br>";
  6. while ($i < count($dateRange)){
  7. //echo $i."<br>";
  8. $athStart = $dateRange[$i]." ".$dayStart;
  9. $athEnd = $dateRange[$i]." ".$dayEnd;
  10. echo $athStart."-".$athEnd."<br>";
  11. $athStmt = $mysqli->prepare($ath_q);
  12. $athStmt->bind_param('ss', $athStart, $athEnd);
  13. $athStmt->execute();
  14. $athResult = $athStmt->get_result();
  15. if ($dATH = $athResult->fetch_column()){
  16. if ($dATH === null){
  17. echo "no value";
  18. $dATH = 0; // Set null value to 0
  19. }
  20. echo $athStart."-".$athEnd."=".$dATH."<br>";
  21. //array_push($dailyATH,$dATH);
  22. }
  23. $i++;
  24. }

当前结果看起来像这样。

  1. 2023-05-01 00:00:00-2023-05-01 23:59:59=1373.2289
  2. 2023-05-02 00:00:00-2023-05-02 23:59:59
  3. 2023-05-02 00:00:00-2023-05-02 23:59:59=2561.0925
  4. 2023-05-03 00:00:00-2023-05-03 23:59:59
  5. 2023-05-03 00:00:00-2023-05-03 23:59:59=6065.2980
  6. 2023-05-04 00:00:00-2023-05-04 23:59:59
  7. 2023-05-05 00:00:00-2023-05-05 23:59:59
  8. 2023-05-05 00:00:00-2023-05-05 23:59:59=3681.4305
  9. 2023-05-06 00:00:00-2023-05-06 23:59:59
  10. 2023-05-06 00:00:00-2023-05-06 23:59:59=8739.7841

如果您注意到在2023-05-04上,没有数据,并且跳到了下一个日期。我正在尝试捕获null并将其设置为0,但似乎我甚至不能捕获null数据。任何帮助将不胜感激。

英文:

I am writing a small web application as a report tool. I have a loop written in to get a value for each day, but I had a problem with our server on one of the days, and data is missing. When I run the query in mySQL, I get NULL returned. I am trying to write some error handling, but it just isn't quite working correctly. What am I doing wrong?

  1. $ath_q=&quot;SELECT SUM(performances*(TIME_TO_SEC(duration)/3600)) AS &#39;ATH&#39;
  2. from streamstats.livestats INNER JOIN streamstats.library USING(artist,song)where ts BETWEEN ? AND ?&quot;;
  3. $i=0;
  4. $dailyATH=[];
  5. echo count($dateRange).&quot;&lt;/br&gt;&quot;;
  6. while ($i &lt; count($dateRange)){
  7. //echo $i.&quot;&lt;/br&gt;&quot;;
  8. $athStart=$dateRange[$i].&quot; &quot;.$dayStart;
  9. $athEnd=$dateRange[$i].&quot; &quot;.$dayEnd;
  10. echo $athStart.&quot;-&quot;.$athEnd.&quot;&lt;/br&gt;&quot;;
  11. $athStmt=$mysqli-&gt;prepare($ath_q);
  12. $athStmt-&gt;bind_param(&#39;ss&#39;,$athStart,$athEnd);
  13. $athStmt-&gt;execute();
  14. $athResult=$athStmt-&gt;get_result();
  15. if ($dATH=$athResult-&gt;fetch_column()){
  16. if ($dATH===null){
  17. echo &quot;no value&quot;;
  18. }
  19. echo $athStart.&quot;-&quot;.$athEnd.&quot;=&quot;.$dATH.&quot;&lt;/br&gt;&quot;;
  20. //array_push($dailyATH,$dATH);
  21. }
  22. $i++;
  23. }

current result looks like this.

  1. 2023-05-01 00:00:00-2023-05-01 23:59:59=1373.2289
  2. 2023-05-02 00:00:00-2023-05-02 23:59:59
  3. 2023-05-02 00:00:00-2023-05-02 23:59:59=2561.0925
  4. 2023-05-03 00:00:00-2023-05-03 23:59:59
  5. 2023-05-03 00:00:00-2023-05-03 23:59:59=6065.2980
  6. 2023-05-04 00:00:00-2023-05-04 23:59:59
  7. 2023-05-05 00:00:00-2023-05-05 23:59:59
  8. 2023-05-05 00:00:00-2023-05-05 23:59:59=3681.4305
  9. 2023-05-06 00:00:00-2023-05-06 23:59:59
  10. 2023-05-06 00:00:00-2023-05-06 23:59:59=8739.7841

If you notice on 2023-05-04, there is no data, and it skips to the next one. I am trying to catch the null and set it to 0, but it seems like I can't even catch the null data. Any help would be greatly appreciated.

答案1

得分: 0

使用空值合并运算符而不是if语句。

  1. $dATH = $athResult->fetch_column() ?? 0;
  2. echo $athStart . "-" . $athEnd . "=" . $dATH . "</br>";
  3. //array_push($dailyATH,$dATH);

你也可以在SQL中这样做:

  1. SELECT IFNULL(SUM(performances*(TIME_TO_SEC(duration)/3600)), 0) AS 'ATH'
英文:

Use the null coalescing operator instead of if statements.

  1. $dATH = $athResult-&gt;fetch_column() ?? 0;
  2. echo $athStart.&quot;-&quot;.$athEnd.&quot;=&quot;.$dATH.&quot;&lt;/br&gt;&quot;;
  3. //array_push($dailyATH,$dATH);

You could also do it in the SQL:

  1. SELECT IFNULL(SUM(performances*(TIME_TO_SEC(duration)/3600)), 0) AS &#39;ATH&#39;

huangapple
  • 本文由 发表于 2023年6月2日 06:32:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定