Bzyzz
Bzyzz

Reputation: 173

PHP Date Variable in MySQL Query

The following query works fine when not using a variable for the date&time. However, I would like to use a variable for "2014-09-29 09:00:00".

    $query = '  SELECT *
                FROM dateTime1
                WHERE dateBooked="2014-09-29 09:00:00"';

(The "dateBooked" field is of datetime format.)

I below doesn't work:

$dateX = '2014-09-29 09:00:00';

$query = '  SELECT *
            FROM dateTime1
            WHERE dateBooked=' .$dateX;

Upvotes: 0

Views: 985

Answers (2)

DeFeNdog
DeFeNdog

Reputation: 1210

Try this:

$dateX = '2014-09-29 09:00:00';
$query = '  SELECT *
                        FROM dateTime1
                        WHERE dateBooked="' . $ dateX . '"';

Upvotes: 1

John Conde
John Conde

Reputation: 219794

You're missing quotes around your date:

$query = "  SELECT *
        FROM dateTime1
        WHERE dateBooked='" .$dateX . "'";

or

$query = "  SELECT *
        FROM dateTime1
        WHERE dateBooked='$dateX'";

Upvotes: 2

Related Questions