<?php
$servername ="localhost";
$username = "root";
$password = "";
$dbname = "myDBPDO";
try{
$conn = new PDO("mysqli:host=$servername;dbname=$dbname;",$username,$password);
//设置PDO错误模式为异常
$conn->serAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//预处理SQL并绑定参数
$stmt= $conn->prepare("INSERT INTO MyGuests(firstname,lastname,email) VALUES(:firstname,:lastname,:email)");
$stmt->bindParam(':firstname',$firstname);
$stmt->bindParam(':lastname',$lastname);
$stmt->bindParam(':email',$email);
//传入参数并执行
$firstname = "Jhon";
$lastname = "Doe";
$email = "jhon@example.com";
$stmt->execute();
$firstname = "Mary";
$lastname = "Moe";
$email = "mary@example.com";
$stmt->execute();
$firstname = "Julie";
$lastname = "Dooley";
$email = "julie@example.com";
$stmt->execute();
echo "新纪录插入成功!";
}catch(PDOException $e){
echo "Error:<br> ".$e->getMessage();
}
$conn=null;
?>