我在学说中使用原始SQL。 我的SQL:
//input datetime type for $dateStart,$dateEnd
$start = date_format( $dateStart,'Y-m-d');
$end = date_format( $dateEnd,'Y-m-d');
$sql = '
SELECT
*
FROM
log
WHERE
time >= "? 00:00:00"
AND
time <= "? 23:59:59"
';
$pdo = $this->em->getConnection();
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1,$start);
$stmt->bindValue(2,$end);
$stmt->execute();
$rs = $stmt->fetchAll();
我试过bindValue,bindParam却没有希望。我还尝试将$param
作为数组传递给execute
,但它不起作用。
$param = array($start,$end);
有人可以帮助我吗?。
答案 0 :(得分:3)
You cannot bind a part of a string literal,但仅限完整的文字
此外,在PHP和SQL之间分割日期创建是没有意义的
$start = date_format( $dateStart,'Y-m-d 00:00:00');
$end = date_format( $dateEnd,'Y-m-d 23:59:59');
$pdo = $this->em->getConnection();
$stmt = $pdo->prepare('SELECT * FROM log WHERE time BETWEEN ? AND ?');
$stmt->execute([$start, $end]);
$rs = $stmt->fetchAll();