为什么PHP DATE_ADD()函数返回语法错误

时间:2014-11-12 03:49:06

标签: php mysql dateadd

我试图通过给出一个日期间隔(以获取记录在一个日期间隔内的行),在同一个数据库中使用一些其他特定表(那里有一组称为table_1,table_2,table_3等的表)来填充表。月)。下面是使用

的代码
<?php
include("functions.php");
connect();
$table_count = mysql_query("SELECT COUNT(*) AS cnt FROM information_schema.tables WHERE table_schema = 'mydb' AND table_name LIKE 'mytable_%'");



$row = mysql_fetch_assoc($table_count);
$loop_var = ($row["cnt"]);


for ($x=1; $x<=$loop_var; $x++) {


mysql_query("INSERT INTO stats (`useragent_browser`, `useragent_OS`, `timestamp`)
SELECT `useragent_browser`, `useragent_OS`, `timestamp` FROM table_'.$x.'
WHERE `timestamp` > DATE_ADD(NOW(), INTERVAL -1 MONTH)") or die(mysql_error());
}
?>

但此刻这会以错误的方式返回

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在&#39;&#39; .1。&#39;。附近使用正确的语法。在哪里timestamp&gt; DATE_ADD(NOW(),INTERVAL -1 MONTH)&#39;在第2行

任何人都可以帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

试试这个:

mysql_query("INSERT INTO stats (`useragent_browser`, `useragent_OS`, `timestamp`)
SELECT `useragent_browser`, `useragent_OS`, `timestamp` FROM table_$x
WHERE `timestamp` > DATE_ADD(NOW(), INTERVAL -1 MONTH)") or die(mysql_error());
}

mysql_query("INSERT INTO stats (`useragent_browser`, `useragent_OS`, `timestamp`)
SELECT `useragent_browser`, `useragent_OS`, `timestamp` FROM table_".$x."
WHERE `timestamp` > DATE_ADD(NOW(), INTERVAL -1 MONTH)") or die(mysql_error());
}