簡體   English   中英

如何處理mysql查詢中的單引號

[英]how can handle single quote in mysql query

我正在用PHP工作。 我想寫這樣的查詢:

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

但是這是由於制造誤差手段,因為單引號的創建問題“。

請建議我該如何處理。 目前我是直接在聖約瑟德父親的研究所上對的,但是在我的PHP代碼中,這是變量$ university_name

所以請建議我如何檢查並消除此類問題。

$location = "St. Josed father's institute";
$location = mysql_real_escape_string($location);
$query = update user_account set university_name='St. Josed father's institute' 
where userId=5;

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

您必須意識到此問題集中在SQL而非php上。 SQL的轉義字符(根據標准92)為'。 所以我們要做的就是改變

'聖 約瑟德父親的研究所”改為“聖 約瑟德父親學院

使用轉義字符,mysql不會將您解釋為字符串的結尾。

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM