PHP-将空值设置为MySQL中没有的日期
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-将空值设置为MySQL中没有的日期,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2922字,纯文字阅读大概需要5分钟。
内容图文
![PHP-将空值设置为MySQL中没有的日期](/upload/InfoBanner/zyjiaocheng/885/0dcbd09ae36243b886f54b5666c9321c.jpg)
因此,我正在使用该代码从MySQL中获取一些数据:
<?
$query=mysql_query("SELECT date,COUNT(*) as num FROM downloads WHERE prjID='".$_GET['id']."' GROUP BY date ORDER BY date ASC");
$num=mysql_num_rows($query);
$res='';
$i=0;
while($row=mysql_fetch_array($query)){
$i++;
$date=date("d.m.Y", strtotime($row['date']));
$dan=date("d", strtotime($row['date']));
$mesec=date("m", strtotime($row['date']));
$leto=date("Y", strtotime($row['date']));
if($i=1){
$danPrvi=$leto.", ".($mesec-1).", ".$dan;
$dan1=date("d", strtotime(time()));
$mesec1=date("m", strtotime(time()));
$leto1=date("Y", strtotime(time()));
$danZadnji=$leto1.", ".($mesec1-1).", ".$dan1;
}
$numb=1;
if($row['num']!=1){
$res.="[Date.UTC(".$leto.",".($mesec-1).",".$dan."),".$row['num']."], ";
}
else{
if($i!=$num){
$res.="[Date.UTC(".$leto.",".($mesec-1).",".$dan."),".$numb."], ";
}
else{
$res.="[Date.UTC(".$leto.",".($mesec-1).",".$dan."),".$numb."]";
}
}
}
?>
我得到这样的结果:
1.3.2013 - 1
6.3.2013 - 5
但我想得到这样的结果:
1.3.2013 - 1
2.3.2013 - 0
3.3.2013 - 0
4.3.2013 - 0
5.3.2013 - 0
6.3.2013 - 1
我也在使用Highcharts,因此日期值的格式必须类似于Date.UTC(year,month-1,day)
编辑:
我的数据库中没有所有日期.在我的例子中,只有1.3.2013和6.3.2013,那么我将如何为所有日期之间的所有不具有> = 1的值检测并设置0值?
解决方法:
我想您需要做的是建立一个结构来保存您感兴趣的日期,并在查询中使用它或对查询数据进行后处理.例如:
<?php
$query=mysql_query("SELECT date,COUNT(*) as num FROM downloads WHERE prjID='".$_GET['id']."' GROUP BY date ORDER BY date ASC");
$num=mysql_num_rows($query);
// Get the first and last dates in the result set
$firstRow = mysql_result($query, 0);
$lastRow = mysql_result($query, $num-1);
// Now make thos the begin and end dates
$beginDate = new DateTime(strtotime($firstRow['date']));
$endDate = new DateTime(strtotime($lastRow['date']));
$currentDate = $beginDate;
$interestingDates = array();
// Populate our interestingDates array with all counts set to 0
while ($currentDate <= $endDate){
$interestingDates[$currentDate->format('d.m.Y')] = 0;
$currentDate->add(new DateInterval('P1D'));
}
// Reset the data result for looping over
mysql_data_seek($query,0);
while($row=mysql_fetch_array($query)){
// Go ahead and format the string
$formatedString = date("d.m.Y", strtotime($row['date']));
// If the string is in our interestingDates array, update the count
if (array_key_exists($formatedString, $interestingDates)){
$interestingDates[$formatedString] = $row['num'];
}
}
// Print it out
foreach ($interestingDates as $key=>$value){
print "$key - $value\n";
}
注1:从PHP 5.5.0开始不推荐使用mysql_query,将来将删除它.请使用其他API-我建议pdo_mysql.
注意2:当前查询未参数化.使用PDO,这可能看起来像:
$sth = $dbh->prepare('SELECT date,COUNT(*) as num FROM downloads WHERE prjID= :prjID GROUP BY date ORDER BY date ASC');
$sth->bindParam(':prjID', $_GET['id'], PDO::PARAM_INT);
$sth->execute();
免责声明-我实际上并没有运行此代码,只是将其写在脑海中.您可能需要对其进行测试/调试.
内容总结
以上是互联网集市为您收集整理的PHP-将空值设置为MySQL中没有的日期全部内容,希望文章能够帮你解决PHP-将空值设置为MySQL中没有的日期所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。