Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 846
lmpearce1 Avatar asked Feb 23 '23 17:02

lmpearce1


2 Answers

It seems like you need to put quotes around the timestamp such as:

SELECT esolar + 3 AS Esolar FROM (calcdata) WHERE siteid = '1' AND time > '2011-10-04 13:02:34'
like image 189
Derek Avatar answered Feb 25 '23 13:02

Derek


i think you just need to change

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

in

->where("time > '$start'");
like image 39
Gianpaolo Di Nino Avatar answered Feb 25 '23 12:02

Gianpaolo Di Nino