实例
<?php
$servername = "localhost";
$username = "root";
$password = "5ac7b1e1ea6eb77f";
$dbname = 'mysqls';
$sql = new mysqli($servername,$username,$password,$dbname);
if($sql->connect_error){
die('数据库连接失败'.$sql->connect_error); //如果连接失败则报错且不往下执行
}
$stmt = $sql->prepare('INSERT INTO mysqls (id,name) VALUES (?,?)'); //初始化$sql并将sql语句模板传值给$stmt
$stmt->bind_param('is',$id,$name);//绑定参数
$id = 1;
$name = 'zhang';
$stmt->execute(); //执行
$id = 2;
$name = 'echo';
$stmt->execute();//执行
$id = 3;
$name = 'php';
$stmt->execute();//执行
echo '插入多组数据成功';
$stmt->close();
$sql->close(); //关闭数据库连接运行结果
数据库截图
在上述的预处理语句当中,我们使用的方法有prerre,bind_param,execute。






还没有评论,来说两句吧...