0

I'm trying to make a select statement using PDO and MySql. I have the following code:

$this->bind[':created'] = "'" . (new DateTime('First day of this month'))->format('Y-m-d') . "' AND '" . date('Y-m-d') . "'"; 
$this->where[] = "created  BETWEEM :created";

As you can see, i'm putting a quote character wrapping the date, following MySql documentation but PHP is escaping it and i'm getting an error.

'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'created BETWEEN '\'2015-07-01\' AND \'2015-07-24\'' '

So, how can i use a range date with PDO::bindValue()?

Being more specific, i want to get this result:

SELECT * FROM table WHERE created BETWEEN '2015-07-01' AND '2015-07-24'

rather than:

SELECT * FROM table WHERE created BETWEEN '\'2015-07-01\' AND '\2015-07-24\''

Thank you.

1 Answers1

4

binding is for SINGLE values. you're trying to bind multiple values. You need:

... WHERE foo BETWEEN :start AND :end

and two bind calls.

Remember, a placeholder is basically kind of like a variable, except the DB knows that this "variable" can only represent a single value. it will NOT pick apart the value you pass in and try to make it fit the statement, so

foo BETWEEN :thingie

is no different than

foo BETWEEEN '$var'

as far as number-of-values-being-inserted is concerned.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • or even `... BETWEEN :created AND:created` no? I to think this yours is clearer - just want to distinguish what actual issue was with what OP was doing. – ficuscr Jul 24 '15 at 20:06
  • 1
    @ficuscr reusing the same placeholder does not work unless emulation mode is on see - http://stackoverflow.com/questions/27461763/phps-pdo-prepared-statement-am-i-able-to-use-one-placeholder-multiple-times and http://stackoverflow.com/questions/2432084/pdo-parameterized-query-reuse-named-placeholders – Sean Jul 24 '15 at 20:09
  • 2
    @ficuscr: what'd be the point of betweening the same value? might as well just have `foo = :created` at that point. – Marc B Jul 24 '15 at 20:10