如何以编程方式使用年月值填充表格列?
这是目标:
DATE
2014-12
2014-11
2014-10
..
2014-02
2014-01
2013-12
2013-11
等,回到设定的日期,例如2008-01
答案 0 :(得分:3)
您可以使用PDO并循环使用年份和月份:
$START_YEAR = 2008;
$END_YEAR = 2012;
$dbh = new PDO('mysql:host=localhost;dbname=mydatabase', $username, $password);
$stmt = $dbh->prepare("INSERT INTO my_table VALUES (?)");
for ($year = START_YEAR; $year <= END_YEAR; ++$year) {
for ($month = 1; $month <= 12; ++$month) {
$stmt->bindParam (1, "${year}-${month}");
$stmt->execute();
}
}