标签:选择 ext 数据库查询 一个 fatal pass cto 面向 mes
PDO(PHP Data Object),是一种以纯面向对象方式实现的数据库操作扩展。
PDO库中有3个类:PDO、PDOStatement、PDOException
PDO常用方法:
PDO::__construct ( string $dsn
[, string $username
[, string $password
[, array $driver_options
]]] )
new PDO(‘mysql:host=localhost;port=3306;dbname=demo‘,‘root‘,‘root‘);
$dsn = ‘mysql:host=localhost;port=3306;dbname=demo‘; $user = ‘root‘; $password = ‘root‘; $pdo = new PDO($dsn, $user, $password); echo ‘<pre>‘; $sql = "set names utf8"; $row = $pdo->exec($sql); var_dump($row); if($row === false){ //因为$row可能为0,故要判断全等 die("SQL Error: {$pdo->errorCode()} {$pdo->errorInfo()[2]}"); } $sql = ‘select * from students‘; $stmt = $pdo->query($sql) or die("SQL Error: {$pdo->errorCode()} {$pdo->errorInfo()[2]}"); var_dump($stmt);
$fetch_style
[, int $cursor_orientation
= PDO::FETCH_ORI_NEXT [, int $cursor_offset
= 0 ]]] )获取结果集所有行:
array PDOStatement::fetchAll ([ int $fetch_style
[, mixed $fetch_argument
[, array $ctor_args
= array() ]]] )
fetch_style:
控制下一行如何返回给调用者,下面是fetch_style的部分取值
function pdo_fatch($stmt,$all = true){ if($all){ return $stmt->fetchAll(PDO::FETCH_ASSOC); }else{ return $stmt->fetch(PDO::FETCH_ASSOC); } }
事务:一种原子操作,批量执行多条命令,只有每一条命令都成功执行才能提交结果,否则回滚结果;
$dsn = ‘mysql:host=localhost;port=3306;dbname=demo‘; $user = ‘root‘; $password = ‘chz‘; $pdo = new PDO($dsn, $user, $password); $pdo->beginTransaction() or die(‘开启事务失败!‘); $pdo->exec("insert into students values(null,‘tst1‘,23)"); //设置回滚点 $pdo->exec(‘savepoint sp1‘); $pdo->exec("insert into students values(null,‘tst2‘,32)"); $pdo->exec(‘rollback to sp1‘); $pdo->commit(); //$pdo->rollback();
也可以直接使用底层SQL语句实现事务
$pdo->exec(‘start transaction‘); //开启事务 $pdo->exec(‘commit‘); //提交事务 $pdo->exec(‘rollback‘); //回滚事务
PDO提供3种错误机制,是通过PDO常量PDO::ATTR_ERRMODE来选择的
PDO::setAttribute()函数可以设置错误处理机制
$dsn = ‘mysql:host=localhost;port=3306;dbname=demo‘; $user = ‘root‘; $password = ‘chz‘; $pdo = new PDO($dsn, $user, $password); $pdo->exec("set names utf8"); //使用默认模式 $sql = "insert int students values()"; $pdo->exec($sql); //不会报错 //切换到警告模式 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING); $sql = "insert int students values()"; $pdo->exec($sql); //给出警告Fatal error: Uncaught PDOException: SQLSTATE[42000] //切换到异常模式 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $sql = "insert int students values()"; try{ $pdo->exec($sql); }catch(PDOException $e){ die("代码运行错误:file:{$e->getFile()} line {$e->getLine()} {$e->getMessage()}"); //异常处理 }
此外也可以在PDO实例化时提供第四个参数设定错误处理模式,数据库连接时也应该进行异常处理:
$dsn = ‘mysql:host=localhost;port=3306;dbname=demo‘; $user = ‘root‘; $password = ‘chz‘; $drivers = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); try{ $pdo = new PDO($dsn, $user, $password, $drivers); }catch(PDOException $e){ die("数据库连接失败!{$e->getMessage()}"); } $sql = "set names utf8"; try{ $pdo->exec($sql); }catch(PDOException $e){ die("代码运行错误:file:{$e->getFile()} line {$e->getLine()} {$e->getMessage()}"); }
常用函数:
占位符:使用mysql原生占位符问号(?)或使用PDO特定预处理参数指令冒号加参数名(:name)
//使用问号占位符占位 $pre_sql = "select * from students where id=?"; $stmt = $pdo->prepare($pre_sql); $stmt->bindValue(1,20); //1表示第一个占位符,20是占位符取值 $stmt->execute(); print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); //execute函数使用索引数组绑定参数 $stmt->execute(array(22)); print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); //使用冒号加参数名占位 $pre_sql = "select * from students where age between :min and :max"; $stmt = $pdo->prepare($pre_sql); //值传递绑定参数方式 $min = 2; $max = 4; $stmt->bindValue(‘:min‘,$min); $stmt->bindValue(‘:max‘,$max); for($i = 0; $i < 3; $i++){ $stmt->execute(); print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); //每次执行结果都一样 $min += 2; $max += 2; } //引用传递绑定参数方式 $min = 2; $max = 4; $stmt->bindParam(‘:min‘,$min); $stmt->bindParam(‘:max‘,$max); for($i = 0; $i < 3; $i++){ $stmt->execute(); print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); //每次执行结果会根据$min和$max变化而变化 $min += 2; $max += 2; } //execute函数使用关联数组绑定参数 $stmt->execute(array(‘:min‘=>3,‘:max‘=>6)); print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
标签:选择 ext 数据库查询 一个 fatal pass cto 面向 mes
原文地址:https://www.cnblogs.com/chuanzi/p/10400073.html