dongpao9437
dongpao9437
2018-04-26 11:28

从mysql中选择时间戳的范围在phpmyadmin中工作,但不在codeigniter中

已采纳

I have read almost every question i have found here and did not get a solution for my problem.

thing is that my query works in phpMyAdmin search but it does not work in codeigniter query.

here is code From phpMyAdmin that works:

SELECT * FROM `pozvani_brojevi` 
WHERE `vrijeme_poziva` 
BETWEEN '2018-04-05 00:00:00' AND '2018-04-26 00:00:00' 
ORDER BY `vrijeme_poziva` DESC

and here is code from codeigniter that don't work:

$this->db->select('*');
$this->db->from('pozvani_brojevi');
$this->db->where('user_id', $user_id);
$this->db->where("'vrijeme_poziva', BETWEEN 2018-04-05 00:00:00 AND 2018-04-26 00:00:00");
$result = $this->db->get();
$pozvani = $result->result_array();
return $pozvani;    

note that i have changed code in various ways and always got same result

$this->db->where("vrijeme_poziva, BETWEEN 2018-04-05 00:00:00 AND 2018-04-26 00:00:00");

$this->db->where("vrijeme_poziva BETWEEN 2018-04-05 00:00:00 AND 2018-04-26 00:00:00");

and i get this error message:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00 AND 2018-04-26 00:00:00' at line 4

SELECT * FROM `pozvani_brojevi` WHERE `user_id` IS NULL AND `vrijeme_poziva` BETWEEN `2018-04-05` `00:00:00` AND 2018-04-26 00:00:00
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douzhi9478 douzhi9478 3年前

    Use Null as second and FALSE as third parameter as below:

    $this->db->where(' vrijeme_poziva BETWEEN 2018-04-05 00:00:00 AND 2018-04-26 00:00:00', NULL, FALSE);
    

    Another Option that you can use >= and <= in two separate where condition as below:

    $this->db->where('vrijeme_poziva >=', '2018-04-05 00:00:00');
    $this->db->where('vrijeme_poziva <=', '2018-04-26 23:59:59');
    
    点赞 评论 复制链接分享
  • douzhi3779 douzhi3779 3年前
    Hi you can use this query.
    
    $this->db->select('*');
    $this->db->from('pozvani_brojevi');
    $this->db->where('user_id', $user_id);
    $this->db->where('vrijeme_poziva >=', date('2018-04-05'));
    $this->db->where('vrijeme_poziva <=', date('2018-04-26'));
    $result = $this->db->get();
    $pozvani = $result->result_array();
    return $pozvani; 
    
    点赞 评论 复制链接分享
  • duan1979768678 duan1979768678 3年前

    You can use query method with your sql query like -

    $query = $this->db->query("SELECT * FROM `pozvani_brojevi` WHERE `vrijeme_poziva` BETWEEN '2018-04-05 00:00:00' AND '2018-04-26 00:00:00' ORDER BY `vrijeme_poziva` DESC");
    

    For more information refer to https://www.codeigniter.com/userguide3/database/queries.html

    点赞 评论 复制链接分享