PHP-使用预处理防止SQL注入
一个简单的例子:
php
1 |
|
预处理以及绑定变量
php
1 | $result = $conn -> prepare("select * from user where username = ? and password = ?"); |
php
1 | $result -> bind_param("ss",$username,$password); |
第一个参数表示参数的类型
- i 表示 整形
- s 表示 字符串
- d 表示 双精度浮点型
- b 表示 二进制大对象
绑定输出结果的变量
php
1 | $result->bind_result($name,$pwd); |
执行查询
php
1 | $result -> excute(); |
获取结果
php
1 | $result = $stmt -> get_result(); |
php
1 | $row = $result -> fetch_assoc(); |
关闭连接
php
1 | $result -> close(); |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.