简体   繁体   English

如何以date_format替换sprintf中的多个%s?

[英]How to replace the multiple %s inside sprintf in a date_format?

I am trying to replace all the %s in the query with the given days. 我正在尝试用给定的日期替换查询中的所有%s But it throws me and error at the Date_format. 但这使我和Date_format.错误Date_format. The %s in the fcst_date is getting replaced correctly. fcst_date%s已正确替换。 Please help 请帮忙

    $day1 = date('Y-m-d');
    $day2 = date('Y-m-d', strtotime("+1 days"));
    $day3 = date('Y-m-d', strtotime("+2 days"));
    $day4 = date('Y-m-d', strtotime("+3 days"));
    $day5 = date('Y-m-d', strtotime("+4 days"));
    $day6 = date('Y-m-d', strtotime("+5 days"));
    $day7 = date('Y-m-d', strtotime("+6 days"));
    $day8 = date('Y-m-d', strtotime("+7 days"));
    $day9 = date('Y-m-d', strtotime("+8 days"));
    $day10 = date('Y-m-d', strtotime("+9 days"));
    $query = sprintf('SELECT blk_id,blk_name,fcst_date,temp_max,max_temp,date,temp_stn_block.stn_id FROM temp_stn_block RIGHT JOIN temp_stn_normals ON temp_stn_block.stn_id=temp_stn_normals.stn_id INNER JOIN block_imd_gfs_forecast ON blk_id=block_id JOIN block_s ON block_s.id=blk_id WHERE DATE_FORMAT(date, "%%m-%%d") IN DATE_FORMAT("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", "%%m-%%d") AND fcst_date IN (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", $day1, $day2, $day3, $day4,$day5, $day6, $day7,$day8,$day9,$day10, $day1, $day2, $day3, $day4,$day5, $day6, $day7,$day8,$day9,$day10);
    $res = $this->db->query($query);
    return $res->result();

I would rather used prepared statements with named param for this just for a more readable code. 我宁愿使用带有命名参数的准备好的语句,只是为了获得更易读的代码。 But then you would have to update your codeigniter db config to use PDO driver. 但是随后您将不得不更新您的codeigniter db配置以使用PDO驱动程序。

For sprintf , just used numbered placeholders: 对于sprintf ,仅使用编号的占位符:


$qry_str = 'SELECT 
 blk_id,blk_name,fcst_date,temp_max,max_temp,date,temp_stn_block.stn_id 
 FROM temp_stn_block 
 RIGHT JOIN temp_stn_normals 
   ON temp_stn_block.stn_id=temp_stn_normals.stn_id 
 INNER JOIN block_imd_gfs_forecast 
   ON blk_id=block_id JOIN block_s ON block_s.id=blk_id 
 WHERE 
   DATE_FORMAT(date, "%%m-%%d") BETWEEN *day1* AND *day10*
 AND 
   fcst_date IN (%1$s, %2$s, %3$s, %4$s, %5$s, %6$s, %7$s, %8$s, %9$s, %10$s)"
';

$query = sprintf($qry_str, $day1, $day2, $day3, $day4, $day5, $day6, $day7, $day8, $day9, $day10);

$res = $this->db->query($query);

I think the issue with your query is the usage of %s which is a specifier for 'seconds' in DATE_FORMAT 我认为您查询的问题是%s的使用情况,这是DATE_FORMAT “秒”的说明符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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