使用PHP插入MySQL数据库的某些字段

时间:2012-03-01 17:42:37

标签: php mysql

我无法正确编译。

$username = mysql_real_escape_string($_GET['username']);
$about = mysql_reaL_escape_string($_GET['about']);
$icebreaker = mysql_reaL_escape_string($_GET['icebreaker']);


$query = "UPDATE '$mysql_database'.main SET about = '$about', icebreaker = '$icebreaker' WHERE username = '$username';";

我收到错误

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 ''a4396957_users'.main 
SET about = 'testing', icebreaker = 'ice' WHERE us' at line 1

此声明中是否缺少某些内容?

2 个答案:

答案 0 :(得分:3)

不要使用撇号或引号引用数据库名称

$query = "UPDATE `$mysql_database`.`main` SET `about` = '$about', `icebreaker` = '$icebreaker' WHERE `username` = '$username';";

使用`而不是'

答案 1 :(得分:2)

$query = "UPDATE $mysql_database.main SET about = '$about', icebreaker = '$icebreaker' WHERE username = '$username';";

MySQL中的某些对象,包括数据库,表,索引,列,别名,视图,存储过程,分区和其他对象名称都称为标识符。

标识符引号字符是反引号(“`”)..

如果启用了ANSI_QUOTES SQL模式,也允许在双引号内引用标识符:

mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax...

mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)