将变量插入mysql表不起作用

时间:2012-05-24 18:13:20

标签: mysql variables insert

我正在尝试将一个变量插入到mysql表中,但它没有做我想要的(没有错误,它根本不起作用)

$string = 'this is an example';
mysql_query("insert into tablename (columnname) values ????");

任何人都可以帮助我如何将$string插入表tablename中?我尝试将????替换为($string)(".$string.")$string,但所有这些都未将$string变量插入数据库。

提前致谢!

2 个答案:

答案 0 :(得分:2)

您可以使用\“(转义的引号)在查询中包含引号,如下所示:

$string = 'this is an example';
mysql_query("insert into tablename (columnname) values (\"$string\")");

答案 1 :(得分:1)

你忘记了引号('),你应该使用双引号作为字符串分隔符。

$string = "test abc";
mysql_query("insert into t (col) values ('$string')");