PHP PDO是PHP Data Objects的簡(jiǎn)稱,它是一種訪問數(shù)據(jù)庫(kù)的通用方法。PDO為每種數(shù)據(jù)庫(kù)提供了一組統(tǒng)一的功能,這意味著開發(fā)人員無需考慮使用不同數(shù)據(jù)庫(kù)API之間的差異,從而使開發(fā)工作變得更加方便。接下來,我們將通過幾個(gè)簡(jiǎn)單的例子來展示如何在PHP中使用PDO連接數(shù)據(jù)庫(kù)。
建立PDO數(shù)據(jù)庫(kù)連接
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "連接成功";
} catch(PDOException $e) {
echo "連接失敗: " . $e->getMessage();
}
?>
在上面的例子中,我們使用三個(gè)參數(shù)連接到數(shù)據(jù)庫(kù),分別是主機(jī)名(localhost)、用戶名(username)和密碼(password)。如果連接失敗,將拋出一個(gè)PDOException異常。
插入數(shù)據(jù)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
$conn->exec($sql);
echo "插入成功";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
在上述示例中,插入了一條新的記錄到名為MyGuests的數(shù)據(jù)庫(kù)表中。如果插入成功,它將輸出一條成功消息。
查詢數(shù)據(jù)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $k=>$v){
print_r("$k -> ".$v['firstname']."<br />");
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
上述示例中,查詢了MyGuests表中的id、firstname和lastname字段。在foreach循環(huán)中,使用fetchAll獲取多行記錄并輸出它們的值。
更新數(shù)據(jù)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=1";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo $stmt->rowCount() . " 行受到影響";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
上述示例中,更新了id為1的記錄,將lastname的值更改為Doe。如果更新成功,將輸出一條成功消息。
刪除數(shù)據(jù)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM MyGuests WHERE id=3";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo $stmt->rowCount() . " 行被刪除";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
在上述示例中,刪除了id為3的記錄。如果刪除成功,將輸出一條成功消息。
總結(jié)
通過使用php pdo連接數(shù)據(jù)庫(kù),我們可以輕松地執(zhí)行許多不同的數(shù)據(jù)庫(kù)任務(wù),從而使我們的工作更加方便。無論您是處理MySQL、SQLite、Oracle還是PostgreSQL,PHP PDO都是您最佳的選擇。