简体   繁体   中英

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM