gpt4 book ai didi

php - 如何在php中将时间转换为字符串

转载 作者:行者123 更新时间:2023-11-29 14:15:37 26 4
gpt4 key购买 nike

我有一个 php foreach 循环,它从不同的数据库中获取数据并将其插入到我的另一个数据库表中。我的问题是当我运行此代码时,我的时间列中出现错误。可能是因为我必须将时间转换为字符串,因为我的表中时间列中的文本除外。

我的错误代码:

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

因此,我将字符串从时间变量中放入,但仍然出现相同的错误。老实说,虽然我不确定这个问题。

我的PHP代码

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();
}

提前致谢;

最佳答案

请更改此行

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

对此

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

关于php - 如何在php中将时间转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com