个人环境
1.Ubuntu
2.LAMP
PHP官方文档
//数据库链接测试
try{
$dsn='mysql:host = localhost;dbname=info'; //数据源
$username = 'root';
$passwd = " ";
$pdo = new PDO($dsn,$username,$passwd);
var_dump($pdo); //返回类型与数值
}catch(PODException $e){
echo $e->getMessage();
}
//EOF是大文本形式
//创建表测试
$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(30) NOT NULL
);
EOF;
$res = $pdo->exec($sql);
var_dump($res);
//插入测试
$sql = 'INSERT user (username,password,email) VALUES ("king","king","[email protected]")';
$res = $pdo->exec($sql);
echo $res;
//插入多条的大文本形式
$sql = <<<EOF
INSERT user (username,password,email) VALUES
("king1","king1","[email protected]"),
("king2","king2","[email protected]"),
("king3","king3","[email protected]")
EOF;
//最后插入记录的编号
$pdo->lastInsertId();
预处理方式
PDOStatement的方法
//查询单一row的测试
$sql = 'select * from user where username="king2"';
$stmt = $pdo->prepare($sql);
$res = $stmt->execute();
$row = $stmt->fetch();
print_r($row);
/**
1.查询多个,可以设置关联,索引,BOTH
2.fetch()设置参数
3.FETCH::ASSOC关联
*/
$sql = 'select * from user ';
$stmt = $pdo->prepare($sql);
$res = $stmt->execute();
if($res){
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
print_r($row);
echo '
';
}
}
//setFetchMode设置返回格式
$stmt->setFetchMode(POD::FETCH_ASSOC);
$rows = $stmt->fetchAll();
print_r($rows);
getAttribute(); 得到数据库连接的属性
setAttribute() ; 设置
//第四个参数可以设置属性
$pdo = new PDO($dsn,$username,$passwd,$options);
$pdo->getAttribute(PDO::ATTR_...);
开始了多文件的交互过程
// web interface
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title> login title>
head>
<body>
<form action='doAction.php' method='post'>
username:<input type='text' name='username' /> <br/>
password:<input type='text' name='password' /> <br/>
<input type='submit' value='login' />
form>
body>
<html>
//doAction.php
header("content-type:text/html;charset=utf-8");
$username = $_POST['username'];
$password = $_POST['password'];
// echo $username ;
// echo $password ;
try{
$pdo = new PDO('mysql:host=localhost;dbname=info','root','');
// var_dump($pdo);
$sql = "select * from user where username='{$username}' and password='{$password}'";
$stmt = $pdo->query($sql);
echo $stmt->rowCount();
}catch(PDOException $e){
$e->getMessage();
}
?>
//通过quote();返回带引号的字符串,过滤字符串的特殊字符
$pdo->quote($username);
$sql="select * from user where username={$username}" and password='{$password}'";
占位符处理方式,预处理…
尽量使用命名参数的占位符,可读性高
// : 占位符,命名占位符
header("content-type:text/html;charset=utf-8");
$username = $_POST['username'];
$password = $_POST['password'];
try{
$pdo = new PDO('mysql:host=localhost;dbname=info','root','');
$sql = "select * from user where username=:username and password=:password";
$stmt = $pdo->prepare($sql);
$stmt—>execute(array(":username"=>$username,":password"=>$password)); echo $stmt->rowCount();
}catch(PDOException $e){
$e->getMessage();
}
?>
// ? 占位符
header("content-type:text/html;charset=utf-8");
$username = $_POST['username'];
$password = $_POST['password'];
try{
$pdo = new PDO('mysql:host=localhost;dbname=info','root',' ');
$sql = "select * from user where username=? and password=?";
$stmt = $pdo->prepare($sql);
$stmt ->execute(array($username,$password));
echo $stmt->rowCount();
}catch(PDOException $e){
$e->getMessage();
}
?>
bindParam() 绑定一个参数到指定的变量名
getColumnMeta() —testing version,索引从0开始
fetchColumn() 没有办法返回同一行的另外一列,每执行一次,指针向下偏移一行,索引从0开始
debugDumpParams() 打印一条预处理命令
//使用命名占位符
$sql = "INSERT user(username,password,email) VALUES(:username,$password,$email)";
$stmt = $pdo -> prepare($sql);
$stmt->bindParam(":username",$username,PDO::PARAM_STR);
$username = "rose";
$stmt->execute();
//使用?
$sql = "INSERT user(username,password,email) VALUES(?,?,?)";
$stmt -> bindParam(1,$username,PDO::PARAM_STR);
bindValue() 把一个值绑定一个参数,这个值可以重复使用
bindValue(3,’[email protected]’);
bindColumn() 绑定一列到一个PHP变量
nextRowset() 存储过程 结果集 指针向下移动一位,用来遍历下一个结果集
多个结果集,首先创建一个存储过程
DELIMITER //
CREATE PROCEDURE test1()
BEGIN
SELECT * FROM user;
SELECT * FROM userAccount;
END
//
DELIMITER ;
错误处理模式
PDO::ERRMODE_SLIENT 默认模式 ,静默模式
PDO::ERRMODE_WARNING 警告模式
PDO::ERRMODE_EXCEPTION 异常模式
PDO事务处理
要么都成功,要么都失败的方式
案例:工资系统
财务 -15000
个人 +15000
事务必须使用innodb的存储引擎
//根据实验场景,创建一个账户表格
CREATE TABLE IF NOT EXISTS userAccount(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE,
money DECIMAL(10,2)
)
INSERT INTO `userAccount`(`username`, `money`) VALUES ('boss',200000),('user1',10000);
//官方手册持久化连接的例子
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
?>