2011-03-09 50 views
0

如何在mysql插入語句中轉義逗號?在插入語句中轉義

mysql> create table test.todel (name varchar(100)); 
Query OK, 0 rows affected (0.03 sec) 

mysql> insert into test.todel values ('mcdonald's pizza'); 
    '> '; 
ERROR 1064 (42000): 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 's pizza'); 
'' at line 1 

mysql> select * from test.todel; 
Empty set (0.02 sec) 

我可以逃避單個逗號,但這不是一個選項,因爲我使用非常複雜的shell腳本插入數據。

mysql> insert into test.todel values ('mcdonald''s pizza'); 
Query OK, 1 row affected (0.00 sec) 

mysql> select * from test.todel; 
+------------------+ 
| name    | 
+------------------+ 
| mcdonald's pizza | 
+------------------+ 
1 row in set (0.00 sec) 

回答

1

使用mysql_real_escape_string來安全地進行掃描。

你的方式不安全順便說一句。

http://www.w3schools.com/php/func_mysql_real_escape_string.asp

+0

請不要鏈接到W3Schools的時候可以[鏈接到官方手冊](http://de3.php.net/manual/en/function.mysql-real -escape-string.php)。 [w3schools已知是有缺陷的](http://w3fools.com) – Gordon 2011-03-10 08:45:33

+0

不知道,謝謝... – fatnjazzy 2011-03-10 12:07:11