• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > PHP如何使用mysqli_real_escape_string()函数?

PHP如何使用mysqli_real_escape_string()函数?

作者:站长图库 字体:[增加 减小] 来源:互联网 时间:2022-04-29

站长图库向大家介绍了PHP如何使用,mysqli_real_escape_string,函数等相关知识,希望对您有所帮助

本篇文章给大家介绍一下PHP使用mysqli_real_escape_string()函数的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。


PHP如何使用mysqli_real_escape_string()函数?


PHP如何使用mysqli_real_escape_string()函数?用法示例

mysqli_real_escape_string()函数是PHP中的内置函数, 用于转义所有特殊字符以用于SQL查询。在将字符串插入数据库之前使用它, 因为它删除了可能干扰查询操作的任何特殊字符。

当使用简单的字符串时, 它们中可能包含特殊字符, 例如反斜杠和撇号(尤其是当它们直接从输入了此类数据的表单中获取数据时)。这些被认为是查询字符串的一部分, 并且会干扰其正常运行。

<?php   $connection = mysqli_connect(     "localhost" , "root" , "" , "Persons" );           // Check connection if (mysqli_connect_errno()) {      echo "Database connection failed." ; }     $firstname = "Robert'O" ;$lastname = "O'Connell" ;    $sql ="INSERT INTO Persons (FirstName, LastName)              VALUES ( '$firstname' , '$lastname' )";        if (mysqli_query( $connection , $sql )) {            // Print the number of rows inserted in     // the table, if insertion is successful     printf( "%d row inserted.n" , $mysqli ->affected_rows);}else {            // Query fails because the apostrophe in      // the string interferes with the query     printf( "An error occurred!" );}    ?>

在上面的代码中, 查询失败, 因为使用mysqli_query()执行撇号时, 会将撇号视为查询的一部分。解决方案是在查询中使用字符串之前使用mysqli_real_escape_string()。

<?php    $connection = mysqli_connect(         "localhost" , "root" , "" , "Persons" );    // Check connection if (mysqli_connect_errno()) {      echo "Database connection failed." ; }         $firstname = "Robert'O" ;$lastname = "O'Connell" ;    // Remove the special characters from the// string using mysqli_real_escape_string    $lastname_escape = mysqli_real_escape_string(                     $connection , $lastname );                       $firstname_escape = mysqli_real_escape_string(                     $connection , $firstname );    $sql ="INSERT INTO Persons (FirstName, LastName)             VALUES ( '$firstname' , '$lastname' )";   if (mysqli_query( $connection , $sql )) {            // Print the number of rows inserted in     // the table, if insertion is successful     printf( "%d row inserted.n" , $mysqli ->affected_rows);}    ?>

输出如下:

1 row inserted.


分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • PHP如何使用mysqli_real_escape_string()函数?
  • PHP如何使用Echarts生成数据统计报表

相关文章

  • 2022-04-29如何用php程序实现网页换肤
  • 2022-04-29Discuz!您当前的访问请求当中含有非法字符,已经被系统拒绝解决办法
  • 2022-04-29Photoshop CC教程:您必须知道的CC新功能
  • 2022-04-29为了使用邮箱服务,php7该如何配置sendmail
  • 2022-04-29Javascript怎么检查对象是否为空
  • 2022-04-29PHPCMSV9采集网址重复的解决办法 array_unique函数
  • 2022-04-29Photoshop制作梦幻光影效果的艺术字教程
  • 2022-04-29浅谈如何在制作dedecms模板中进行一些SEO设置
  • 2022-04-29搜索引擎优化技巧_英文关键字篇
  • 2022-04-29浅谈小程序中页面间传值的2种方法

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • MySQL怎么修改一列的值
    • PHP如何设置过期时间的session
    • 使用css实现自适应标题浮动效果(代码实例)
    • 正则表达式解决input框固定输入值的格式(金额,特殊字符)
    • 手把手教你使用Vue3实现图片散落效果
    • YII怎么输出sql语句?
    • 如何优化css expression性能
    • PhotoShop绘制水晶质感3D立体按钮制作教程
    • Photoshop制作高光梦幻效果的艺术字教程
    • 被遗忘的WordPress模板标签有哪些?

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有