PDO介紹
PHP數據對象(PDO)擴展爲PHP訪問數據庫定義了一個輕量級的一致接口。實現PDO接口的每個數據庫驅動可以公開具體數據庫的特性作爲標準擴展功能。注意利用PDO擴展自身並不能實現任何數據庫功能;必須使用一個具體數據庫的PDO驅動
來訪問數據庫服務。
PDO提供了一個數據訪問抽象層,這意味着,不管使用哪種數據庫,都可以用相同的函數(方法)來查詢和獲取數據。 PDO不提供數據庫抽象層;它不會重寫 SQL,也不會模擬缺失的特性。如果需要的話,應該使用一個成熟的抽象層。
操作MySQL數據庫需要PDO_MYSQL
驅動,支持MySQL 3.x/4.x/5.x。
創建PDO對象
PDO構造規則是
PDO::__construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )
其中對於dsn(Data Source Name),包含如下元素:
- DSN前綴,對於PDO_MYSQL,固定爲
mysql:
- host,主機名
- post,主機端口
- dbname,數據庫名稱
- unix_socket,MySQL Unix socket(不能和host或port同時使用)
- charset,字符集
例子1
<?php
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
?>
例子2
<?php
$dsn = 'mysql:host=localhost;port=12345;dbname=testdb;charset=utf8';
$username = 'username';
$password = 'password';
$dbh = new PDO($dsn, $username, $password);
?>
查詢
例子1——查詢多條記錄
<?php
$dsn = 'mysql:host=192.168.1.101;port=3306;dbname=zhyoulun;charset=utf8';
$username = 'zhyoulun';
$password = 'Abcd#1234';
$pdo = new PDO($dsn,$username,$password);
$sql = 'select id,name from a_tag limit 0,3';
$statement = $pdo->prepare($sql);
if(!$statement->execute())
{
echo "execute error";
exit(1);
}
$items = $statement->fetchAll();
print_r($items);
輸出
Array
(
[0] => Array
(
[id] => 1
[0] => 1
[name] => yii2
[1] => yii2
)
[1] => Array
(
[id] => 2
[0] => 2
[name] => 僞靜態
[1] => 僞靜態
)
[2] => Array
(
[id] => 3
[0] => 3
[name] => php
[1] => php
)
)
例子2——查詢多條記錄
$sql = 'select id,name from a_tag limit 0,3';
$statement = $pdo->prepare($sql);
if(!$statement->execute())
{
echo "execute error";
exit(1);
}
while($item = $statement->fetch())
{
print_r($item);
}
結果
Array
(
[id] => 1
[0] => 1
[name] => yii2
[1] => yii2
)
Array
(
[id] => 2
[0] => 2
[name] => 僞靜態
[1] => 僞靜態
)
Array
(
[id] => 3
[0] => 3
[name] => php
[1] => php
)
例子3——綁定參數
$sql = 'select id,name from a_tag where id>=:id_min and id<=:id_max';
$statement = $pdo->prepare($sql);
$statement->bindValue(':id_min', 3);
$statement->bindValue(':id_max', 4);
if(!$statement->execute())
{
echo "execute error";
exit(1);
}
while($item = $statement->fetch(PDO::FETCH_ASSOC))
{
print_r($item);
}
結果
Array
(
[id] => 3
[name] => php
)
Array
(
[id] => 4
[name] => curl
)
插入
例子
$sql = "insert into a_tag(name,count) values('標籤1',0);";
$affectRows = $pdo->exec($sql);
print_r($affectRows);
結果
1
更新
例子
$sql = "update a_tag set count=2 where id=1;";
$affectRows = $pdo->exec($sql);
print_r($affectRows);
結果
1
刪除
例子
$sql = "delete from a_tag where id=41;";
$affectRows = $pdo->exec($sql);
print_r($affectRows);
結果
1
事務
例子
$pdo->beginTransaction();
try{
for($i=1;$i<100000;$i++)
{
$sql = "insert into a_tag(id,name,count) values({$i},'標籤2',0);";
$affectRows = $pdo->exec($sql);
var_dump($affectRows);
//觸發回滾
if($i==100)
throw new PDOException();
}
$pdo->commit();
} catch (PDOException $e) {
echo "rollback";
$pdo->rollBack();
}
運行可以可以看到循環了100次,100次以後,觸發回滾,數據庫中沒有新增數據。
參考
- PHP Manual