2020年6月4日
<?php
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
$sql="SELECT * FROM a";
$zhi=$pdo->query($sql);
if($zhi){
echo "执行成功";
echo "<pre>";
$fetch_data=[];
while ($row=$zhi->fetch(PDO::FETCH_ASSOC)){
$fetch_data[] = $row;
}
var_dump($fetch_data);
echo"</pre>";
}else{
echo"执行失败";
}
}catch(PDOException $e){
var_dump($e->getMessage());
}
//设置错误报告
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a";
$zhi=$pdo->query($sql);
if($zhi){
echo"执行成功";
}else{
var_dump($pdo->errorInfo()[2]);
}
}catch(PDOException $e){
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
//余额表
//CREATE TABLE a(
// n VARCHAR(15) NOT NULL,
// r INT(10)
// )ENGINE=innodb;
//INSERT INTO a VALUES('夏亚','1000'),('阿姆罗','1000');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
$sql="UPDATE a SET rmb=rmb-200 WHERE name='夏亚'";
$pdo->exec($sql);
$sql="UPDATE a SET rmb=rmb+200 WHERE name='阿姆罗'";
$pdo->exec($sql);
$pdo->commit();
}catch(PDOException $e){
var_dump($e->getMessage());
}
//预处理流程
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a";
$zhi=$pdo->prepare($sql);
$res=$zhi->execute();
if($res){
$tl=$zhi->fetchAll(PDO::FETCH_ASSOC);
var_dump($tl);
}else{
echo"执行失败";
}
}catch(PDOException $e){
$pdo->rollback();
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a WHERE name LIKE ? AND rmb=?;";
$zhi=$pdo->prepare($sql);
$s="%红%";
$r="1000";
$zhi->bindParam(1,$s);
$zhi->bindParam(2,$r);
$zhi->bindValue(1,"%子%");
$zhi->bindValue(2,3200);
$res=$zhi->execute();
if($res){
var_dump($zhi->fetchAll(2));
}
}catch(PDOException $e){
$pdo->rollback();
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a WHERE name LIKE :search AAND rmb = :qian;";
$zhi=$pdo->prepare($sql);
$s="%子%";
$qian=3200;
$zhi->bindParam('s',$s);
$zhi->bindValue('qian',3200);
$res=$zhi->execute();
if($res){
var_dump($zhi->fetchAll(2));
}catch(PDOException $e){
var_dump($e->getMessage());
}
<?php
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
$sql="SELECT * FROM a";
$zhi=$pdo->query($sql);
if($zhi){
echo "执行成功";
echo "<pre>";
$fetch_data=[];
while ($row=$zhi->fetch(PDO::FETCH_ASSOC)){
$fetch_data[] = $row;
}
var_dump($fetch_data);
echo"</pre>";
}else{
echo"执行失败";
}
}catch(PDOException $e){
var_dump($e->getMessage());
}
//设置错误报告
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a";
$zhi=$pdo->query($sql);
if($zhi){
echo"执行成功";
}else{
var_dump($pdo->errorInfo()[2]);
}
}catch(PDOException $e){
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
//余额表
//CREATE TABLE a(
// n VARCHAR(15) NOT NULL,
// r INT(10)
// )ENGINE=innodb;
//INSERT INTO a VALUES('夏亚','1000'),('阿姆罗','1000');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
$sql="UPDATE a SET rmb=rmb-200 WHERE name='夏亚'";
$pdo->exec($sql);
$sql="UPDATE a SET rmb=rmb+200 WHERE name='阿姆罗'";
$pdo->exec($sql);
$pdo->commit();
}catch(PDOException $e){
var_dump($e->getMessage());
}
//预处理流程
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a";
$zhi=$pdo->prepare($sql);
$res=$zhi->execute();
if($res){
$tl=$zhi->fetchAll(PDO::FETCH_ASSOC);
var_dump($tl);
}else{
echo"执行失败";
}
}catch(PDOException $e){
$pdo->rollback();
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a WHERE name LIKE ? AND rmb=?;";
$zhi=$pdo->prepare($sql);
$s="%红%";
$r="1000";
$zhi->bindParam(1,$s);
$zhi->bindParam(2,$r);
$zhi->bindValue(1,"%子%");
$zhi->bindValue(2,3200);
$res=$zhi->execute();
if($res){
var_dump($zhi->fetchAll(2));
}
}catch(PDOException $e){
$pdo->rollback();
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a WHERE name LIKE :search AAND rmb = :qian;";
$zhi=$pdo->prepare($sql);
$s="%子%";
$qian=3200;
$zhi->bindParam('s',$s);
$zhi->bindValue('qian',3200);
$res=$zhi->execute();
if($res){
var_dump($zhi->fetchAll(2));
}catch(PDOException $e){
var_dump($e->getMessage());
}
<?php
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
$sql="SELECT * FROM a";
$zhi=$pdo->query($sql);
if($zhi){
echo "执行成功";
echo "<pre>";
$fetch_data=[];
while ($row=$zhi->fetch(PDO::FETCH_ASSOC)){
$fetch_data[] = $row;
}
var_dump($fetch_data);
echo"</pre>";
}else{
echo"执行失败";
}
}catch(PDOException $e){
var_dump($e->getMessage());
}
//设置错误报告
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a";
$zhi=$pdo->query($sql);
if($zhi){
echo"执行成功";
}else{
var_dump($pdo->errorInfo()[2]);
}
}catch(PDOException $e){
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
$pdo=new PDO($dsn,"root","root");
//余额表
//CREATE TABLE a(
// n VARCHAR(15) NOT NULL,
// r INT(10)
// )ENGINE=innodb;
//INSERT INTO a VALUES('夏亚','1000'),('阿姆罗','1000');
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
$sql="UPDATE a SET rmb=rmb-200 WHERE name='夏亚'";
$pdo->exec($sql);
$sql="UPDATE a SET rmb=rmb+200 WHERE name='阿姆罗'";
$pdo->exec($sql);
$pdo->commit();
}catch(PDOException $e){
var_dump($e->getMessage());
}
//预处理流程
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a";
$zhi=$pdo->prepare($sql);
$res=$zhi->execute();
if($res){
$tl=$zhi->fetchAll(PDO::FETCH_ASSOC);
var_dump($tl);
}else{
echo"执行失败";
}
}catch(PDOException $e){
$pdo->rollback();
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a WHERE name LIKE ? AND rmb=?;";
$zhi=$pdo->prepare($sql);
$s="%红%";
$r="1000";
$zhi->bindParam(1,$s);
$zhi->bindParam(2,$r);
$zhi->bindValue(1,"%子%");
$zhi->bindValue(2,3200);
$res=$zhi->execute();
if($res){
var_dump($zhi->fetchAll(2));
}
}catch(PDOException $e){
$pdo->rollback();
var_dump($e->getMessage());
}
try{
$dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
$pdo=new PDO($dsn,"root","root");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM a WHERE name LIKE :search AAND rmb = :qian;";
$zhi=$pdo->prepare($sql);
$s="%子%";
$qian=3200;
$zhi->bindParam('s',$s);
$zhi->bindValue('qian',3200);
$res=$zhi->execute();
if($res){
var_dump($zhi->fetchAll(2));
}catch(PDOException $e){
var_dump($e->getMessage());
}
笔记:
设置PDO的选项:setAttribute()
设置列名强制大写:pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);
数据库里面设置的字段是大写php获取到就是大写pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
设置强制列名小写:pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
结果类别
PDO::FETCH_ASSOC 结果设置未关联数组
PDO::FETCH_BOTH 结果只为混合数组
PDO::FETCH_NUM 结果只为索引数组
fetch(结果类别);获取一条数据;类似于mysql的写法
事物处理:事物处理是用来保证数据操作的原子性(成功或失败)
保证多个sql语句处理全部成功则数据修改生效,如果其中任何一个语句出差那所有的语句修改都不生效
事物处理对数据存储引擎有要求
MyISAM 不支持事物
innodb 支持事物
PDO预处理 预先处理数据步骤:
1.发送sql语句给预处理2.执行预处理的sql语句
参数绑定
sql语句在开发中,比如搜索功能;通常搜索值都是由外传递进来的;
sql注入:如果用户是黑客,输入一些特殊的代码,服务器如果没处理直接丢到sql语句发送给mysql执行可能会被黑掉