KingRichard
KingRichard

Reputation: 1244

PHP mySQL query not working with variable as in field for SQL TIME type

One of the fields in the mysql database is 'TIME' type, formatted 'H:i:s'. When querying the database with the field value set, like:

$result = $wpdb->get_var($wpdb->prepare("SELECT * FROM table WHERE start_hour='06:00:00'", ARRAY_A));

Everything works great. But when I replace it with a variable like this:

$stime = '06:00:00';
$result = $wpdb->get_var($wpdb->prepare("SELECT * FROM table WHERE start_hour=$stime", ARRAY_A));

$result comes up empty. I've been messing about with the formatting of $stime, but am coming up empty.

Upvotes: 0

Views: 91

Answers (1)

Jerska
Jerska

Reputation: 12042

You're missing single quotes around $stime in your second query :

$stime = '06:00:00';
$result = $wpdb->get_var($wpdb->prepare("SELECT * FROM table WHERE start_hour='$stime'", ARRAY_A));

Upvotes: 1

Related Questions