I want to execute a MySQL Query in my Wordpress PHP.
In the Query I used WHERE CLAUSE with Date, and for that I declared $date variable before $result.
$date = date("'Y-m-d'") ;
$result = $wpdb->get_results( "SELECT Date, Winner, Time FROM Winners WHERE Date <= $date");
My Question is How to write this in single line that is without declaring $date variable.
I tried this, but didn't work.
$result = $wpdb->get_results( "SELECT Date, Winner, Time FROM Winners WHERE Date <= " date("'Y-m-d'")) ;
How to fix this ..?
CodePudding user response:
There are two ways.
1. By MySQL CURDATE() Function.
$result = $wpdb->get_results( "SELECT Date, Winner, Time FROM Winners WHERE Date <= CURDATE()" ) ;
2. By PHP date() Function
$result = $wpdb->get_results( "SELECT Date, Winner, Time FROM Winners WHERE DateC <=" . date("'Y-m-d'") ) ;
Hope this Helps.