最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

$wpdb prepare issue with mysql DATE_FORMAT

programmeradmin2浏览0评论

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

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

Share Improve this question asked Apr 8, 2014 at 18:07 Tamil Selvan CTamil Selvan C 3977 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论