stact overflowsam1
stact overflowsam1

Reputation: 77

How to convert time to string in php

I have a php foreach loop which fetch data from a different database and Insert it into my other database table. My problem is when I ran this code I got an error in my time column. Maybe because I have to convert the time to a string as my table excepts text in time column.

My error code:

SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "22"
SELECT 897196,2018-02-26 22:20:09,1...
                         ^ - error

So, I put string in from of the time variable, but still the same error. Though I am not sure about the problem to be quite honest.

My Php code

foreach($results as $n){

$id = $n['ID'];
$time = (string)$n['Time']=='0000-00-00 00:00:00'?null:(string)$n['Time'];
$email= $n['email'];
$marks= $n['marks'];
$rank= $n['rank'];


$psql->get_db()->beginTransaction();


    $stmt = $psql->pdo_prepared("INSERT INTO student(
        id, time,email, marks, rank) SELECT $id,$time, $email,$marks,$rank FROM student WHERE NOT EXISTS (SELECT 1 FROM student WHERE id = $id AND time = $time AND email= $email AND marks = $marks AND rank= $rank)");

$psql->get_db()->commit();
}

Thank you in Advance;

Upvotes: 0

Views: 67

Answers (1)

Midhun
Midhun

Reputation: 1167

Please change this line

$time = (string)$n['Time']=='0000-00-00 00:00:00'?null:(string)$n['Time'];

to this

$time = (string)$n['Time']=='0000-00-00 00:00:00'  ? null : '"'.(string)$n['Time'] .'"';

Upvotes: 3

Related Questions