12.PHP_PDO数据库抽象层
PDO數(shù)據(jù)庫(kù)抽象層
? ? ?
?????其實(shí)也就是說(shuō),為了方便PHP項(xiàng)目各種數(shù)據(jù)庫(kù)切換的方便以及代碼兼容性,再各種數(shù)據(jù)庫(kù)上又封裝了一層,做成了統(tǒng)一的接口,方便數(shù)據(jù)庫(kù)使用和切換。
PDO鏈接mysql數(shù)據(jù)庫(kù):
<?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變量:
打開(kāi)數(shù)據(jù)庫(kù),進(jìn)入表,把c1列的內(nèi)容拿出來(lái)
<?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.
把上面的例子改成一起全都取出來(lái),然后在枚舉:
<?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中事物處理:
還有很多函數(shù),用的時(shí)候再查吧,注意PDO里面是支持錯(cuò)誤捕獲,警告設(shè)置,異常模式,錯(cuò)誤處理等。
總結(jié)
以上是生活随笔為你收集整理的12.PHP_PDO数据库抽象层的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Win64 驱动内核编程-16.WFP网
- 下一篇: Win64 驱动内核编程-17. MIN