使用PHP时出现奇怪的语法错误MySQL错误

时间:2015-04-25 18:14:29

标签: php mysql syntax-error

我在这段代码中的第1行附近有语法错误:

//...
$nr = 126;
$expl = "'test'";
$da = "'2003-12-01'";
$tax = 2.5;
$cost = 100;

//Here is the error
$sql = "INSERT INTO tbl (type, nr, explan, date, tax, cost) VALUES('Example'," + $nr + ",'"+ $expl + "','" + $da + "'," + $tax + "," + $cost + ");";

这是错误日志:

Error: 103.5
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 '103.5' at line 1

我使用的是PHP版本5.6.3和MySQL版本5.6.21

我无法弄清楚,问题是什么:(

1 个答案:

答案 0 :(得分:2)

您正在使用+来连接字符串,这在PHP中不起作用。您必须使用.

可能有一些奇怪的算术会产生103.5,就像2.5 + "," + 100一样。将","转换为int(1)或其他内容的位置。