简体   繁体   中英

PHP/mysql - My timestamp does not like to be in a greater than (>) statement

I've got a timestamp in my database with the value: 2011-10-05 16:06:48 which is bigger than the $start variable ( 2011-10-04 13:02:34 ) I have defined yet when I run the query I get an error message.

function getgeneration() {

$period = '1 month';
$siteid = 1;
$start = '2011-10-04 13:02:34';

$value = $this->GetOffset();

$this->db->select("esolar + $value AS Esolar")
     ->from('calcdata')
     ->where('siteid', $siteid)
     ->where("time > $start");
$query = $this->db->get()->result_array();
$Esolar1 = $query[0]['Esolar'];
echo $Esolar1;
return $Esolar1;

}
 A Database Error Occurred 

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 '13:02:34' at line 4

SELECT esolar + 3 AS Esolar FROM (calcdata) WHERE siteid = 1 AND time > 2011-10-04 13:02:34

Filename: /var/www/test/models/blog_model.php

Line Number: 220

Is this a common problem with timestamps?

看起来您需要在时间戳周围加上引号,例如:

SELECT esolar + 3 AS Esolar FROM (calcdata) WHERE siteid = '1' AND time > '2011-10-04 13:02:34'

i think you just need to change

->where("time > $start");

in

->where("time > '$start'");

Put the timestamp in single or double quotes. They are strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM