Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter每天从数据库查询数据_Php_Mysql_Codeigniter - Fatal编程技术网

Php Codeigniter每天从数据库查询数据

Php Codeigniter每天从数据库查询数据,php,mysql,codeigniter,Php,Mysql,Codeigniter,我随身带着一个$from date和$to date,因此我想在$from和$to之间的几天内从数据库中提取四个值,包括$from和$to。如果数据在数据库中有一天不存在,则必须将零作为缺失值的值 那么,我如何在codeigniter中编写一个查询来实现它,并且相应的日期应该存储在特定行的结果中呢 希望这有帮助 $this->db->where('date_column >= ',$date_given_start); $this->db->where('date_

我随身带着一个
$from date
$to date
,因此我想在
$from
$to
之间的几天内从数据库中提取四个值,包括
$from
$to
。如果数据在数据库中有一天不存在,则必须将零作为缺失值的值

那么,我如何在codeigniter中编写一个查询来实现它,并且相应的日期应该存储在特定行的结果中呢

希望这有帮助

$this->db->where('date_column >= ',$date_given_start);
$this->db->where('date_column <= ',$date_given_end);
$this->db->get('TABLE_NAME')->result();


// if column is datetime
$this->db->where('DATE(date_column) >= ',$date_given_start);
$this->db->where('DATE(date_column) <= ',$date_given_end);
$this->db->get('TABLE_NAME')->result();
$this->db->where('date\u column>=',$date\u given\u start);
$this->db->where('date\u column=',$date\u given\u start);

$this->db->where('DATE(DATE\u column)我以前的解决方案是使用PHP检查并设置缺失日期的零。但是我有一些新的解决方案,您可以试试

  • 创建一个表来存储日期,并与表进行左/右连接
  • 或使用存储过程和联接创建临时表。临时表将在会话过期时自动删除
  • 将UNION与select语句一起使用
  • 关于StackOverflow有很多答案

    我认为您的解决方案需要实际使用PHP,并且不确定是否可以通过一个查询直接从MYSQL获取所需内容。据我所知,您希望运行一个查询,获取定义日期范围内的所有记录,然后在没有记录的日期中有一个空行(或者使用您决定的任何其他值…)

    实际上,我将运行与您相同的查询,用于选择daterange之间的行,并用于生成开始日期和结束日期之间所有日期的数组

    $begin = new DateTime( '2012-08-01' );
    $end = new DateTime( '2012-10-31' );
    $end = $end->modify( '+1 day' );
    
    $interval = new DateInterval('P1D');
    $daterange = new DatePeriod($begin, $interval ,$end);
    
    foreach($daterange as $date){
        echo $date->format("Y-m-d") . "<br>";
    }
    

    希望这能让您走上正确的道路……

    我想这个问题会为您提供一个解决方案,您的数据库表的结构是什么?即使没有所需的值,您是否也有每个日期的记录?不,我的记录中没有每个日期以及日期,它以什么格式存储在数据库中?对不起,我已经知道查询y您已经编写了,但它不会为数据库中缺少的日期返回零值。因此,请仔细阅读我的问题并回答。Minh Quy我明白了,但我不想为它创建表。我想使用现有数据库。我的数据库中没有每个日期。我看到了您的问题。您可以创建临时表来存储每个日期en
    $from
    $to
    日期。第二种方法,您可以使用for循环PHP创建UNION语句和concat with normal语句。或者使用我的愚蠢解决方案,在打印/回显结果时使用PHP检查缺少的日期哈哈..!对您的答案完全满意。我将使用第二种方法,而不是愚蠢的解决方案,但这是一个错误同样正确。首先,我希望我有机会投票给一个人两次。
    // set the start & end dates
    $from_date = '2012-09-11';
    $to_date     = '2012-11-11';
    
    // create a daterange object
    $begin         = new DateTime($from_date);
    $end           = new DateTime($to_date );
    $end            = $end->modify( '+1 day' );
    $interval      = new DateInterval('P1D');
    $daterange = new DatePeriod($begin, $interval ,$end);
    
    $sth = $dbh->prepare("SELECT col1, dateCol from tb WHERE dateCol>'".$from_date."' AND dateCol<'".$to_date."' order by dateCol ");
    $sth->execute(); 
    $rows = $sth->fetchAll(PDO::FETCH_ASSOC);
    
    $rowsByDay = array(); // will hold the rows with date keys
    
    // loop on results to create thenew rowsByDay
    foreach($rows as $key=>$row) {
        $rowsByDay[strtotime($row['dateCol'])][] = $row; // add the row to the new rows array with a timestamp key
    }
    
    // loop of the daterange elements and fill the rows array 
    foreach($daterange as $date){
        if(!isset($rowsByDay[strtotime($date->format("Y-m-d"))])) // if element does not exists - meaning no record for a specific day
        {
            $rowsByDay[strtotime($date->format("Y-m-d"))] = array(); // add an empty arra (or anything else)
        }
    }
    
    // sort the rowsByDay array so they all are arrange by day from start day to end day
    ksort($rowsByDay); 
    
    
    // just for showing what we get at the end for rowsByDay array
    foreach ($rowsByDay as $k=>$v) {
        echo date('Y-m-d',$k);
        var_dump($v);
        echo '<hr/>';
    }