1.1 建表
<?php
try {
$dsn = 'mysql:host=localhost;dbname=test';
$username = 'root';
$password = '123456';
$pdo = new PDO($dsn,$username,$password);
//exec();执行一条 sql 语句,并返回受影响的记录的条数,如果没有受影响返回0
//exec 对 select 没有作用
$sql = <<<EOF
create table if not exists user(
id int unsigned auto_increment key,
username varchar(20) not null unique,
password char(32) not null,
email varchar(32) not null
)
EOF;
$res = $pdo->exec($sql);
var_dump($res);
} catch (PDOException $e) {
echo $e->getMessage();
}
返回 0;
插入记录:
//插入一条记录
//$sql = 'insert user(username,password,email) values("wjh",md5("wjh"),"[email protected]")';
//$res = $pdo->exec($sql); 返回 1
//插入多条记录
$sql = <<<EOF
insert user(username,password,email) values("wjh1",md5("wjh"),"188@qq.com"),
("wjh2",md5("wjh"),"188@qq.com"),
("wjh3",md5("wjh"),"188@qq.com")
EOF;
$res = $pdo->exec($sql); // 返回 3
$pdo->lastInsertId(); //返回 最后一条插入记录的 ID
更新记录:
$sql = 'update user set username="immoc" where id=3';
$res = $pdo->exec($sql);
删除记录
$sql = 'delete from user where id=2';
$res = $pdo->exec($sql);
获取错误信息:
//删除记录
$sql = 'delete from user12 where id=2';
$res = $pdo->exec($sql);
if ($res === false) {
//返回数据库句柄上一次操作相关的SQLSTATE的值
echo $pdo->errorCode();
echo '<br/>';
//返回数据库句柄上一次操作错误信息的数组,数组包括3个单元
//1. 0 => SQLSTATE
//2. 1 => 错误编号,CODE
//3. 3 => 信息
$errInfo = $pdo->errorInfo();
var_dump($errInfo);
} else {
var_dump($res);
}
2. query()
try {
$pdo = new PDO('mysql:host=localhost;dbname=test','root','123456');
//$sql = 'select * from user where id=2;';
$sql = 'select * from user;';
//执行 sql 语句,返回PDOStatement对象
$stmt = $pdo->query($sql);
foreach ($stmt as $row) {
echo $row['id'];
echo '<br/>';
echo $row['username'];
echo '<br/>';
echo $row['password'];
echo '<br/>';
echo $row['email'];
echo '<br/>';
}
} catch (PDOException $e) {
echo $e->getMessage();
}
3.prepare() and execute()
try {
$pdo = new PDO('mysql:host=localhost;dbname=test','root','123456');
$sql = "select * from user";
//返回一个 PDOStatement 对象
//prepare($sql);准备 sql 语句
$stmt = $pdo->prepare($sql);
//设置默认的返回模式
$stmt->setFetchMode(PDO::FETCH_OBJ);
// execute(); 执行预处理语句,返回布尔值
$res = $stmt->execute();
//取出结果集取出一条记录
// $row = $stmt->fetch();
if ($res) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($row );
}
}
//用 fetchAll()
/*$rows = $stmt->fetchAll(); var_dump($rows);*/
} catch (PDOException $e) {
echo $e->getMessage();
}
4.getAttribute() and setAttribute()
try {
$dsn = 'mysql:host=localhost;dbname=test';
$username = 'root';
$password = '123456';
$pdo = new PDO($dsn,$username,$password);
// 默认为 1 ,自动提交
echo '自动提交 : ' . $pdo->getAttribute(PDO::ATTR_AUTOCOMMIT);
echo '<br/>';
echo '默认错误处理模式 :' . $pdo->getAttribute(PDO::ATTR_ERRMODE);
echo '<br/>';
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
echo '自动提交 : ' . $pdo->getAttribute(PDO::ATTR_AUTOCOMMIT);
} catch (PDOException $e) {
echo $e->getMessage();
}
<?php try { $dsn = 'mysql:host=localhost;dbname=test'; $username = 'root'; $password = '123456'; $pdo = new PDO($dsn,$username,$password); $attArr = array( 'AUTOCOMMIT', 'CASE', 'CLIENT_VERSION', 'CONNECTION_STATUS', 'DRIVER_NAME', 'ERRMODE', 'ORACLE_NULLS', 'PERSISTENT', 'PREFETCH', 'SERVER_INFO', 'SERVER_VERSION', 'TIMEOUT' ); foreach ($attArr as $attr) { echo "PDO_ATTR_$attr : "; echo $pdo->getAttribute(constant("PDO::ATTR_$attr")); echo '<br/>'; } } catch (PDOException $e) { echo $e->getMessage(); }