开发者

Inserting a name with ' in a MySQL table

First time with this trouble when dealing with a MySQL table.

I'm inserting bar names in a table. If the bar is called "Tim's Bar" and I insert it straight away I get an error and the data is not inserted.

How do you开发者_如何转开发 instert properly the ' in the table?


Use mysql_real_escape_string():

http://php.net/manual/en/function.mysql-real-escape-string.php


Use PDO with prepared statements.

$query = $pdo->prepare('INSERT INTO bars (name) VALUES (?)');
$query->execute("Tim's Bar");

It's superior (and safer) than using the mysql(i)_* family of functions directly.


INSERT INTO your_table SET person_name = 'Tim\'s Bar';

Note the \'


addslashes() by the insert, and stripslashes() by the output would also work


I believe you should insert it as 'Tim\'s Bar'.

Regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜