PHP mysqli_real_escape_string() 函數(shù)
PHP MySQLi 參考手冊
實例
轉義字符串中的特殊字符:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"CREATE TABLE myPersons LIKE Persons");
$newpers="Da'Silva"
// This query will fail, cause we did not escape $newpers
mysqli_query($con,"INSERT into myPersons (Lastname) VALUES ('$newpers')");
$newpers=mysqli_real_escape_string($con,$newpers);
// This query will work, cause we escaped $newpers
mysqli_query($con,"INSERT into myPersons (Lastname) VALUES ('$newpers')");
mysqli_close($con);
?>
定義和用法
mysqli_real_escape_string() 函數(shù)轉義在 SQL 語句中使用的字符串中的特殊字符。
語法
mysqli_real_escape_string(connection,escapestring);
參數(shù) | 描述 |
connection | 必需。規(guī)定要使用的 MySQL 連接。 |
escapestring | 必需。要轉義的字符串。編碼的字符是 NUL(ASCII 0)、\n、\r、\、'、" 和 Control-Z。 |
技術細節(jié)
返回值: | 返回已轉義的字符串。 |
PHP 版本: | 5+ |
PHP MySQLi 參考手冊
更多建議: