12.PHP_PDO数据库抽象层
PDO數據庫抽象層
? ? ?
?????其實也就是說,為了方便PHP項目各種數據庫切換的方便以及代碼兼容性,再各種數據庫上又封裝了一層,做成了統一的接口,方便數據庫使用和切換。
PDO鏈接mysql數據庫:
<?php
????header("Content-type:?text/html;?charset=utf-8");
????$dbms?=?'mysql';
????$dbName='abc';
????$user?=?'root';
????$pwd?=?'123456';
????$hosts='localhost';
????$dsn?=?"$dbms:host=$hosts;dbname=$dbName";
????try{
????????$pdo?=?new?PDO($dsn?,$user?,$pwd);
????????echo?"PDO?連接?MySql?成功";
????}catch(Exception?$e){
????????echo?$e->getMessage()."<br>";
????}
?>
?
注意上面的dsn變量:
打開數據庫,進入表,把c1列的內容拿出來
<?php
????header("Content-type:?text/html;?charset=utf-8");
????$dbms?=?'mysql';
????$dbName='abc';
????$user?=?'root';
????$pwd?=?'123456';
????$hosts='localhost';
????$dsn?=?"$dbms:host=$hosts;dbname=$dbName";
????try{
????????$pdo?=?new?PDO($dsn?,$user?,$pwd);
????????$query?=?"select?*?from?tb_222";
????????$result?=?$pdo->prepare($query);
????????$result->execute();
????????while($res?=?$result->fetch(PDO::FETCH_ASSOC))?{
????????????echo?$res['c1']?.?'<br>';
????????}
????}catch(Exception?$e){
????????echo?$e->getMessage()."<br>";
????}
?>
5.
把上面的例子改成一起全都取出來,然后在枚舉:
<?php
????header("Content-type:?text/html;?charset=utf-8");
????$dbms?=?'mysql';
????$dbName='abc';
????$user?=?'root';
????$pwd?=?'123456';
????$hosts='localhost';
????$dsn?=?"$dbms:host=$hosts;dbname=$dbName";
????try{
????????$pdo?=?new?PDO($dsn?,$user?,$pwd);
????????$query?=?"select?*?from?tb_222";
????????$result?=?$pdo->prepare($query);
????????$result->execute();
????????$res?=?$result->fetchAll(PDO::FETCH_ASSOC);
????????for($i?=?0?;$i?<?count($res)?;$i?++){
????????????echo?$res[$i]['c1'].'<br>';
????????}
????}catch(Exception?$e){
????????echo?$e->getMessage()."<br>";
????}
?>
?
PDO中事物處理:
還有很多函數,用的時候再查吧,注意PDO里面是支持錯誤捕獲,警告設置,異常模式,錯誤處理等。
總結
以上是生活随笔為你收集整理的12.PHP_PDO数据库抽象层的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win64 驱动内核编程-16.WFP网
- 下一篇: Win64 驱动内核编程-17. MIN