牛牛在线工具

mysql_escape_string()

收录 8868 个 PHP 函数,含用途说明与用法示例。

mysql_escape_string()

对字符串进行转义,以防止在插入到MySQL数据库中时出现意外的字符

函数名:mysql_escape_string() 适用版本:PHP 4, PHP 5 用法: mysql_escape_string()函数用于对字符串进行转义,以防止在插入到MySQL数据库中时出现意外的字符。 语法: string mysql_escape_string ( string $unescaped_string ) 参数: unescaped_string:需要转义的字符串。 返回值: 返回一个转义后的字符串。 示例: // 连接到MySQL数据库 $connection = mysql_connect("localhost", "username", "password"); mysql_select_db("database", $connection); // 假设要插入的字符串为 "It's a test" $string = "It's a test"; // 使用mysql_escape_string()函数进行转义 $escaped_string = mysql_escape_string($string); // 执行插入语句 $query = "INSERT INTO table (column) VALUES ('$escaped_string')"; mysql_query($query, $connection); // 关闭数据库连接 mysql_close($connection); 注意事项: mysql_escape_string()函数已在PHP 5.5.0中弃用,并且在PHP 7.0.0中已被移除。建议使用mysqli_real_escape_string()或PDO的预处理语句来代替。 在PHP 5.4.0及更高版本中,如果未连接到MySQL数据库,mysql_escape_string()函数将返回false。 为了更好地保护数据库安全,建议使用预处理语句或使用mysqli_real_escape_string()函数来转义字符串。