2011-12-13 58 views
1

我正在使用php。我想写这样的查询: -如何处理mysql查询中的单引号

update user_account set university_name='St. Josed father's institute' 
where userId=5; 

,但是这是由于意味着它,因为单引号的创建问题“产生错误。

请给我建议我该如何处理。目前我对这个直接圣Josed父亲的研究所但在我的PHP代码,这是变量 $ UNIVERSITY_NAME

所以请建议我我如何检查并删除此类类型的问题。

+2

重复的很多这些:http://stackoverflow.com/search?q=mysql+escape+quote –

+0

http://dev.mysql.com/doc/refman/5.0/en/string-literals.html –

回答

11
$location = "St. Josed father's institute"; 
$location = mysql_real_escape_string($location); 
3

你可以使用PHP函数mysql_real_escape_string

<?php 
// Connect 
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') 
    OR die(mysql_error()); 

// Query 
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'", 
      mysql_real_escape_string($user), 
      mysql_real_escape_string($password)); 
?> 

文献在此:http://php.net/manual/en/function.mysql-real-escape-string.php

4
$query = update user_account set university_name='St. Josed father's institute' 
where userId=5; 

$query = str_replace("'","''", $query); 

这是必要的,你意识到身边SQL,而不是PHP这个问题的中心。 SQL的转义字符(根据标准92)是'。所以我们想要做的是改变

'圣。 Josed父亲学院'到'圣。 Josed father''s institute'

使用转义字符,mysql不会将您解释为字符串的结尾。