Christian  H
Christian H

Reputation: 108

PDO BIND a DATE MYSQL - How to pass a date ?

I have a problem

When i use this in PHP:

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

I get an error with the :date

but if i instead of :date write '2016-1-14' it works like a charm.

how do i pass a time string in PDO ?

I get this error:

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}

If i put '' around :date in the query i get this error:

exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in /Applications/XAMPP/xamppfiles/htdocs/getData.php:63 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/getData.php(63): PDOStatement->bindParam(':date', '2016-1-14', 2) #1 {main}

I am a newbie when we talk about PDO

Thanks in advance!

Upvotes: 0

Views: 5265

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

Actually I think I see the issue, just a typo probably when you replaced a hard coded date with the parameter

This line

"WHERE b_date=:date"

need a space after :date like this

"WHERE b_date=:date "

Upvotes: 2

Related Questions