2

I have several db calls in my site with bind_variables that works fine. But, I can find the correct sign for Date in the documentation, for the command:

$query->bind_param("ssi",...);

I don't want to do something like:

$db->query('SELECT item FROM table WHERE something='.$something);

Since this is string manipulation, not binding. (In binding the query is left with the "?" and that makes the queries faster because the DB sees them the same only with different cariables.)

If I wasn't very clear, I want to do the same as this only with a date variable type.

Community
  • 1
  • 1
AYBABTU
  • 986
  • 3
  • 17
  • 39
  • 1
    If I do understand correctly have a look at: http://stackoverflow.com/questions/805828/using-mysqli-bind-param-with-date-and-time-columns. It looks like you can just treath it as a string. – Rene Terstegen Jan 27 '11 at 11:16
  • But then you insert the date as a string. Is that good practice? – AYBABTU Jan 27 '11 at 11:17

3 Answers3

1

Some extra information to my comment given above:

If I do understand correctly have a look at: Using Mysqli bind_param with date and time columns?. It looks like you can just treath it as a string.

If you want to do it with bind_param it is the only way I know to do it and I don't see any problems. If mysql receives a wrong formatted date it will insert a 0000-00-00 value to your table.

Can you tell me what you think could be a problem? If you insert it as a normal query you also use the same syntax as a String.

Community
  • 1
  • 1
Rene Terstegen
  • 7,911
  • 18
  • 52
  • 74
0

For dates, you will have to format them before calling bind_param:

$query->bind_param('s', $date->format('Y-m-d H:i:s')); // assuming $date is a DateTime object
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
0

Dates should be bound as strings in a format MySQL accepts (yyyy-mm-dd).

Quassnoi
  • 413,100
  • 91
  • 616
  • 614