TMA
TMA

Reputation: 33

Issue with timestamp in a query

I have this piece of code which i use to compare a timestamp field with a date i've selected before! But i get an error in syntax.. Howcome?

$query = sprintf( 'SELECT * FROM coupon WHERE date("Y-m-d", "time") = $date' );

The error is:

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 ' "time") = $date' at line 1

While, i'm comparing a timestamp which is the field time, and a date which is $date Thanks in advance..

Upvotes: 0

Views: 61

Answers (2)

feeela
feeela

Reputation: 29922

You have mixed up the PHP function

date ( string $format [, int $timestamp = time() ] )

and the MySQL function.

DATE( expr )

See the manual entries respectively:

http://php.net/manual/en/function.date.php

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 72981

time should not be quoted as it is a field reference, not a string literal.

$query = sprintf('SELECT * FROM coupon WHERE DATE(time) = $date');

Note: You are susceptible to SQL Injection.

Upvotes: 1

Related Questions