PDO绑定日期MYSQL - 如何通过日期?

时间:2016-01-14 00:15:25

标签: php mysql pdo

我遇到了问题

当我在PHP中使用它时:

<!--Try using header

like this:-->

if($_POST)  
{  
  header('location:login-form.php');  
}
else
{
  echo "";
}

我收到了错误:date

但如果我代替:date write&#39; 2016-1-14&#39;它就像一个魅力。

如何在PDO中传递时间字符串?

我收到此错误:

$stmt1 = $conn->prepare("SELECT COUNT(b_slotStart) FROM cp_booking "
                . "WHERE b_date=:date"
                . "AND :start BETWEEN cp_booking.b_slotStart "
                . "AND cp_booking.b_slotEnd "
                . "OR :stop BETWEEN cp_booking.b_slotStart AND cp_booking.b_slotEnd");

        $stmt1->bindParam(':start',$time->format('H:i'), PDO::PARAM_STR);
        $stmt1->bindParam(':stop',$time->add($interval)->format('H:i'), PDO::PARAM_STR);
        $stmt1->bindParam(':date',$date, PDO::PARAM_STR);
        $stmt1->execute();
        $slotStart = $stmt1->fetchColumn(); 

如果我把&#39;&#39;周围:查询中的日期我收到此错误:

exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '? BETWEEN cp_booking.b_slotStart AND cp_booking.b_slotEnd OR ? BETWEEN cp_bookin' at line 1' in /Applications/XAMPP/xamppfiles/htdocs/getData.php:59 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/getData.php(59): PDO->prepare('SELECT COUNT(b_...') #1 {main}

当我们谈论PDO时,我是新手

提前致谢!

1 个答案:

答案 0 :(得分:2)

实际上我认为我看到了这个问题,可能只是在你用参数

替换硬编码日期时的错字

这一行

"WHERE b_date=:date"

需要:date之后的空格

"WHERE b_date=:date "