背景
因为MYSQL已被弃用,原有一些老的项目大量用到原生mysql_xxx相关的函数,不能预处理和参数绑定,很多拼接的sql有注入风险,如何升级到mysqli或pdo呢
因为mysqli是mysql的增强版本,MySQLi 的 i 代表 Improvement,更稳定更高效更安全,所以升级到mysqli是成本最低,风险最小,效果最好的解决方案
现在我们就总结一下如何升级到mysqli,前面是对应的mysql库相关的函数,后面是对应的mysqli相关函数,有些参数不一致,需要调整下
区别和联系
MySQL 每次链接都会打开一个连接的进程。
MySQLi 多次运行将使用同一连接进程,减少了服务器的开销。
详细改动对比
mysql_connect mysqli_connect 参数不一致
mysql_connect(server,user,pwd,newlink,clientflag) mysqli_connect(host,username,password,dbname,port,socket);
mysql_select_db mysqli_select_db 参数不一致
mysql_select_db(database,connection) mysqli_select_db(connection,dbname)
mysql_query mysqli_query 需要一个链接参数,mysql_query原来的后面可能会有链接参数,需要删除
mysql_query(query,connection) mysqli_query(connection,query,resultmode);
可以直接替换的
mysql_fetch_assoc mysqli_fetch_assoc
mysql_num_rows mysqli_num_rows
mysql_num_fields mysqli_num_fields
mysql_fetch_object mysqli_fetch_object
需要适当调整的
mysql_fetch_array mysqli_fetch_array
其中第二个参数常量可能需要替换
mysql_fetch_array(data,array_type)
MYSQL_ASSOC MYSQLI_ASSOC
MYSQL_NUM MYSQLI_NUM
MYSQL_BOTH MYSQLI_BOTH
可以直接替换或者加上链接参数
mysql_affected_rows(link_identifier) mysqli_affected_rows(connection)
mysql_insert_id mysqli_insert_id
mysql_insert_id(connection) mysqli_insert_id(connection);
mysql_fetch_field mysqli_fetch_field
mysql_fetch_field(data,field_offset) mysqli_fetch_field(result);
mysql_close mysqli_close
mysql_close(link_identifier) mysqli_close(connection);
mysql_escape_string mysql_real_escape_string 替换 mysqli_escape_string mysqli_real_escape_string
mysql_escape_string(link_identifier) mysql_real_escape_string(link_identifier) mysqli_escape_string(connection,escapestring); mysqli_real_escape_string(connection,escapestring);
其他的再补充...
更多参考:
mysql :https://www.w3school.com.cn/php/php_ref_mysql.asp
mysqli : https://www.runoob.com/php/php-ref-mysqli.html
防注入代码
<?php //定义配置文件 $config = [ //地址 "host" => "127.0.0.1", //数据库名称 "dbname" => "xxxx", //用户名 "user" => "xxxx", //密码 "pass" => "xxx" ]; //连接数据库 $link = mysqli_connect($config["host"],$config["user"],$config["pass"],$config["dbname"]); //获取用户id 使用intval防注入 $user_id = intval($_GET["id"]); $user_id = $_GET["id"]; $user_name = $_GET["name"]; //写sql语句 $sql = "select * from p_users where user_id=? and user_name=?"; echo "<br>".var_dump($sql); //预处理 $stmt = mysqli_prepare($link,$sql); //绑定函数 这里的ss为 后面第一个参数为字符串 第二个参数也为字符串 mysqli_stmt_bind_param($stmt,"ss",$user_id,$user_name); //执行 mysqli_stmt_execute($stmt); //获取结果 $res = mysqli_stmt_get_result($stmt); //结果转换为二维数组 $res = mysqli_fetch_all($res,1); echo "<pre>";print_r($res); echo "<pre>";
2个工具辅助
您可以从这里下载一个转换器工具:https://github.com/philip/MySQLConverterTool
它生成的代码是非常大的,主要是因为它使用$GLOBAL变量实现默认数据库链接参数的方式. (这也使得很容易识别什么时候有人正在使用通过转换器的代码.)
还有一个MySQL Shim库位于这里:
https://github.com/dshafik/php7-mysql-shim
- 本文固定链接: https://www.phpmianshi.com/?id=267
- 转载请注明: admin 于 PHP面试网 发表
《本文》有 0 条评论