时间:2021-07-01 10:21:17 帮助过:4人阅读
$servername = 'servername'; //服务器名称
$username = 'username'; //数据库用户名
$password = 'password'; //登录密码
$dbname = 'mydb';
//创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
//检测连接(PHP 5.2.9和5.3.0版本适用)
/* if($conn->connect_error){
die('连接失败:' . $conn->connect_error);
} */
//检测连接(兼容更早版本)
if(mysqli_connect_error()){
die('连接失败:' . mysqli_connect_error());
}
else{
$sql = "INSERT INTO myguests VALUES(?, ?, ?)";
//为mysqli_stmt_prepare()初始化statement对象
$stmt = mysqli_stmt_init($conn);
echo $stmt; //不知道为什么$stmt是空的,没有数据
//预处理语句
if(mysqli_stmt_prepare($stmt, $sql)){
//绑定参数
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
//设置参数并执行
$firstname = 'Sam';
$lastname = 'Poly';
$email = [email protected]';
mysqli_stmt_execute($stmt);
$firstname = 'Mary';
$lastname = 'Moe';
$email = [email protected]';
mysqli_stmt_execute($stmt);
$firstname = 'Julie';
$lastname = 'Dooley';
$email = [email protected]';
mysqli_stmt_execute($stmt);
echo '插入成功';
}
}