1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| $server = 'localhost'; $username = 'root'; $pwd = '123456'; $conn = new PDO("mysql:host=$server;dbname=test",$username,$pwd);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql = 'create database myDB'; $conn->exec($sql);
$sql = "create table myGuests(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,firstname VARCHAR(30) NOT NULL,lastname VARCHAR(10) NOT NULL,email VARCHAR(50),reg_date TIMESTAMP)"; $conn->exec($sql);
$sql = "INSET INTO myGuests (firstname,lastname,email) VALUES('John', 'Doe', 'john@example.com')"; if($conn->query === true){ echo '插入成功'; }
$conn->beginTransaction(); $sql = "insert into myguests (firstname,lastname,email) values ('John','lin','666666@qq.com')"; $conn->exec($sql); $sql = "insert into myguests (firstname,lastname,email) values ('lei','li','99999999@qq.com')"; $conn->exec($sql); $conn->commit(); echo '数据插入成功'; else{ $conn->rollBack(); }
$stmt = $conn->prepare("insert into myguests (firstname,lastname,email) value (:firstname,:lastname,:email)"); $stmt->bindParam(':firstname',$firstname); $stmt->bindParam(":lastname",$lastname); $stmt->bindParam('email',$email);
$firstname = 'john'; $lastname = 'doe'; $email = '1111@qq.com'; $stmt->execute(); $firstname = "Mary"; $lastname = "Moe"; $email = "mary@example.com"; $stmt->execute(); echo '插入成功';
$stmt = $conn->query("SELECT * FROM MyGuests"); while($row = $stmt->fetch()){ var_dump($row); echo "<br />"; }
`select * from myguests where firstname = josn`;
`select * from myGuests order by email`
`select * from myguests set email='111@qq.com' where firstname='join' and lastname='wang'`
`delete from myguests where lastname='wang'`
$conn = null;
|