mysql数据库SQL语法错误php

时间:2013-06-24 12:40:34

标签: php mysql syntax

我收到消息:

You have an error in your SQL syntax;check the manual that corresponds 
to your MySQL server version for the right syntax to use near
'12:00,25.32,1015.19,42.32,200.0,1.29,2.25,7.11,11.58,5.97,3.5,0.0,0.0,0.0,0.0,37'
at line 2

相关代码:

$con = mysql_connect("127.0.0.1", "username", "password") or die(mysql_error());
mysql_select_db("database",$con) or die (mysql_error());

//readFmi() returns an array of strings with values separated by commas
$array =  readFmi($xml);

$cols = 'date, Temperature, Pressure, Humidity, WindDirection, WindSpeedMS,
MaximumWind, WindGust, DewPoint, TotalCloudCover, LowCloudCover, MediumCloudCover,
HighCloudCover, Precipitation1h, PrecipitationAmount, RadiationLW, RadiationGlobal,
RadiationNetTopAtmLW';

foreach($array as $a){
  $a2 = mysql_real_escape_string($a);
  echo $a2."<br>";
  mysql_query("INSERT INTO forecastsFMI ($cols)
  VALUES ($a2)") or die (mysql_error());
}

mysql_close($con);

$ a打印时看起来像这样

2013-06-24 12:00,25.32,1015.19,42.32,200.0,1.29,2.25,7.11,11.58,5.97,3.5,0.0,0.0,0.0,0.0,375.85,550.09,-260.6

$ cols与数据库中的表格完全相同,我现在将它们全部设置为varchar,同时试图弄清楚我犯了哪些错误。我一直在计算数据库中的字段和值以查看我是否出错,尝试更改字符串本身的格式并尝试更改和调整代码的不同部分,结果没有任何差异。可能导致这种情况的原因是什么?

谢谢 //托拜厄斯

1 个答案:

答案 0 :(得分:2)

您需要在字符串中添加字符串/日期列值的引号。所以在你的情况下,你需要一些类似的东西。

"2013-06-24 12:00",25.32,1015.19,42.32,200.0,1.29,2.25,7.11,11.58,5.97,3.5,0.0,0.0,0.0,0.0,375.85,550.09,-260.6