PHP mysqli_real_escape_string() 函数

PHP 教程 · 2019-01-28 10:59:09

转义字符串中的特殊字符:

<?php 
// 假定数据库用户名:root,密码:123456,数据库:CODERCTO 
$con=mysqli_connect("localhost","root","123456","CODERCTO"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 

mysqli_query($con,"CREATE TABLE websites2 LIKE websites");

$newname="菜鸟'教程";

// 没有转义 $newname 中特殊字符,执行失败
mysqli_query($con,"INSERT into websites2 (name) VALUES ('$newname')");

// 转义特殊字符
$newpers=mysqli_real_escape_string($con,$newname);

// 转义后插入,执行成功
mysqli_query($con,"INSERT into websites2 (name) VALUES ('$newname')");

mysqli_close($con);
?>

定义和用法

mysqli_real_escape_string() 函数转义在 SQL 语句中使用的字符串中的特殊字符。

语法

mysqli_real_escape_string(connection,escapestring);

参数 描述
connection 必需。规定要使用的 MySQL 连接。
escapestring 必需。要转义的字符串。编码的字符是 NUL(ASCII 0)、\n、\r、\、'、" 和 Control-Z。

技术细节

返回值: 返回已转义的字符串。
PHP 版本: 5+

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

Sprint

Sprint

Jake Knapp、John Zeratsky、Braden Kowitz / Simon & Schuster / 2016-3-8 / GBP 14.60

媒体推荐 “Every business leader I know worries about the same thing: Are we moving fast enough? The genius of Jake Knapp’s Sprint is its step-by-step breakdown of what it takes to solve big problems an......一起来看看 《Sprint》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

MD5 加密
MD5 加密

MD5 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具