EDIT:
The issue is not about having a comma after TIMESTAMP
as suggested in the comment. TIMESTAMP
is not a variable and it is a Postgres keyword to convert the time and date to a timestamp data type before inserting it. Similar to a cast.
I have the following SQL query in Postgres and it works with no errors:
INSERT INTO sms (from_number, to_number, message, reply_time, sent_at) VALUES ('10000000000', '0000000000', 'ffffffff', now(), TIMESTAMP '2015-05-27 18:45:31')
I have a function in PHP with the equivalent query:
function insertWithSentTime($from, $to, $text, $sentTime)
{
$con = pg_connect("host=$this->host dbname=$this->db user=$this->user password=$this->pass") or die ("Could not connect to server\n");
$query = "INSERT INTO sms (from_number, to_number, message, reply_time, sent_at) VALUES ($1, $2, $3, now(), TIMESTAMP $4)";
pg_prepare($con, "prepare1", $query) or die ("Cannot prepare statement\n");
pg_execute($con, "prepare1", array($from, $to, $text, $sentTime)) or die ("Cannot execute statement\n");
pg_close($con);
}
When the function is called I get the following message:
PHP Warning: pg_prepare(): Query failed: ERROR: syntax error at or near "$4" LINE 1: ...eply_time, sent_at) VALUES ($1, $2, $3, now(), TIMESTAMP $4) ^
What am I doing wrong here?