您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php mysql 转义的方法

2024/6/25 0:07:47发布29次查看
php mysql转义方法:1、用mysql库函数“mysql_escape_string”或“mysqli_real_escape_string”进行转义,语法“mysql_escape_string($str)”或“mysqli_real_escape_string($str)”;2、用转义函数“addslashes()”进行转义,语法“addslashes($str)”。
本文操作环境:windows7系统、php8、dell g3电脑。
php访问mysql数据库对字符串进行特殊字符转义
转义特殊字符:单引号(')、双引号()、反斜线(\),以便于进行数据库查询
方法一:利用mysql库函数
php版本在7.0之前:
mysql_escape_string ( string $unescaped_string ) : string
php版本在7.0之后:
mysqli_real_escape_string ( mysqli $link , string $escapestr ) : string
方法二:利用转义函数addslashes()
适合版本php4、php5、php7
addslashes ( string $str ) : string
php 5.4 之前 php 指令 magic_quotes_gpc 默认是 on, 实际上所有的 get、post 和 cookie 数据都用被 addslashes() 了。 不要对已经被magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。 遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。即get_magic_quotes_gpc()返回false时,再使用addslashes()进行特殊字符转义。示例如下:
function myaddslashes($data){ if(false == get_magic_quotes_gpc()) { return addslashes($data);//未启用魔术引用时,转义特殊字符 } return $data;}
推荐:《php视频教程》
以上就是php mysql 转义的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product