2011-12-13 59 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不會將您解釋爲字符串的結尾。