php bind_param()函数
来源:互联网 发布:王小二果园淘宝店铺 编辑:程序博客网 时间:2024/06/05 05:27
从字面上不难理解,绑定的参数;下面我通过一个绑定参数的例子讲一下:
for example:
bind_param(“sss”,
firstname, lastname, $email);1:该函数绑定了 SQL 的参数,且告诉数据库参数的值。 “sss” 参数列处理其余参数的数据类型。s 字符告诉数据库该参数为字符串。
参数有以下四种类型:
i - integer(整型)
d - double(双精度浮点型)
s - string(字符串)
b - BLOB(布尔值)
每个参数都需要指定类型。
通过告诉数据库参数的数据类型,可以降低 SQL 注入的风险。2:上面的
firstname, lastname, $email传的是引用,在php5.3之后是不能直接写成字符串的,为了验证这个结论,在此我写了一段测试,如下:
$servername="localhost";$username="root";$password="admin";$dbname="test";$conn=new mysqli($servername,$username,$password,$dbname);if($conn->connect_error){ die("connected failed:".$conn->connect_error);}$sql="INSERT INTO user(user_first,user_last,age)VALUES(?,?,?)";$stmt=$conn->prepare($sql);$stmt->bind_param("sss","xiao","hong",22);$stmt->execute();echo "News records created successfully!";$stmt->close();$conn->close();
上面我写了一段将参数直接写成字符串的测试程序,运行之后弹出:
最后我将程序改写为,如下:
$servername="localhost"; $username="root"; $password="password"; $dbname="test"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error){ die("Connect failed:".$conn->connect_error); } $sql="INSERT INTO user(user_first,user_last,age)VALUES(?,?,?)"; $stmt=$conn->prepare($sql); $stmt->bind_param("sss",$user_first,$user_last,$age); $user_first="xiao"; $user_last="hong"; $age=12; $stmt->execute(); echo "News records created successfully!"; $stmt->close(); $conn->close();
而上面这段程序可以正常执行;
0 0
- php bind_param()函数
- bind_param()解析
- mysql绑定参数bind_param原理以及防SQL注入
- mysql绑定参数bind_param原理以及防SQL注入
- Parameter 2 to mysqli_stmt::bind_param() expected to be a reference
- mysql使用bind_param()参数绑定来防止SQL注入攻击
- php函数
- php函数
- PHP函数
- php函数
- PHP 函数
- php函数
- PHP 函数
- PHP函数
- PHP 函数
- PHP函数
- PHP函数
- php函数
- POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)
- 数据结构【线性表(二)链表】项目之单链表:逆置
- unity官方demo学习之Stealth(四)屏幕渐变效果
- 关于Homebrew的一些错误总结
- 配置mysql5.5主从服务器
- php bind_param()函数
- VBA 第9课 合并单元格
- Spring MVC Cookie example
- 冒泡排序法
- 数据结构【线性表(二)链表】项目之单链表:连接
- iOS编程-------事件处理UIEvent / 触摸事件UITouch
- Rich控件一
- mysql.exe闪退与ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' 的解决方案
- 10 种机器学习算法的要点(附 Python 和 R 代码)