• 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()函数?

作者:站长图库 字体:[增加 减小] 来源:互联网

站长图库向大家介绍了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生成数据统计报表

相关文章

  • php构造方法和java构造方法有什么区别
  • Apache Log4j 2.17.0已发布!看看解决了什么问题?
  • 如何使用微信获取openid的静默及非静默
  • phpcms显示验证码出错怎么办?
  • ThinkPHP中自定义错误、成功、异常提示页面的方法
  • WordPress如何创建page页面并添加跳转链接
  • 搜索引擎营销中作弊的方式—链接作弊
  • Discuz!教程之删除注释云平台JS,加快Discuz访问
  • 纯css实现gif动图生成字画符
  • 分享一个顺丰同城配送的扩展包并在laravel中使用

文章分类

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

最近更新的内容

    • 详解Bootstrap中的手风琴效果
    • Photoshop制作超酷的树藤装饰立体字教程
    • Bootstrap中怎么实现加载效果?读取图标(Spinners)组件
    • 宝塔2.x面板文件不小心被误删或损坏的修复方法
    • ThinkPHP6通过Ucenter实现注册登录的示例代码
    • DedeCMS使用sql语句获取文章链接地址
    • Photoshop制作个性的透明玻璃效果牌
    • PHP如何去掉所有HTML标签?
    • 符合W3C标准的网页等于做了百分之五十的SEO工作
    • 百度频繁变更算法,我们该怎么办?

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

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