3

Today, I have 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 DATE_FORMAT. How i solved this issue in wordpress

1 Answer 1

5

From the Codex page for the WPDB class:

[...] the prepare method [...] supports both a sprintf()-like and vsprintf()-like syntax.

Having a look at PHP's documentation for sprintf():

##Example 6 ...

   // notice the double %%, this prints a literal '%' character

So you can use

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

for your purposes.

1
  • Yea this works, I've done something similar using LIKE in a WHERE clause. Commented Apr 8, 2014 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.