4

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
lmpearce1
  • 179
  • 2
  • 12
  • just quote the value, you're passing it as a string. – JohnP Oct 12 '11 at 15:49
  • Consult the database manual of your framework about parametrized queries. You are probably using Codeigniter: [Does Code Igniter automatically prevent SQL injection?](http://stackoverflow.com/q/1615792/367456) – hakre Dec 11 '12 at 15:17

3 Answers3

7

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'
Derek
  • 4,864
  • 5
  • 28
  • 38
1

i think you just need to change

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

in

->where("time > '$start'");
Gianpaolo Di Nino
  • 1,139
  • 5
  • 17
0

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

Biotox
  • 1,563
  • 10
  • 15