Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: $wpdb prepare fails when using mysql DATE_FORMAT

Tags:

wordpress

Today, I had a issue in $wpdb.

I used

$result = $wpdb->get_var( 
  $wpdb->prepare(
    "SELECT DATE_FORMAT(report_date, '%d-%m-%Y') FROM table WHERE report_id = %d",
     $report_id 
   )
);

The above code not works because of %d in mysql DATE_FORMAT.

How i solved this issue in wordpress.

like image 844
Tamil Selvan C Avatar asked Apr 08 '14 18:04

Tamil Selvan C


1 Answers

Try this:

$result = $wpdb->get_var( 
  $wpdb->prepare(
    "SELECT DATE_FORMAT(report_date, '%%d-%%m-%%Y') FROM table WHERE report_id = %d",
     $report_id 
   )
);

You need to escape the %'s by using %%

like image 180
EdgeCaseBerg Avatar answered Oct 16 '22 18:10

EdgeCaseBerg