基本的 PDO 連線和檢索
自 PHP 5.0 起, PDO 已作為資料庫訪問層使用。它與資料庫無關,因此以下連線示例程式碼只需更改 DSN 即可適用於任何受支援的資料庫 。
// First, create the database handle
//Using MySQL (connection via local socket):
$dsn = "mysql:host=localhost;dbname=testdb;charset=utf8";
//Using MySQL (connection via network, optionally you can specify the port too):
//$dsn = "mysql:host=127.0.0.1;port=3306;dbname=testdb;charset=utf8";
//Or Postgres
//$dsn = "pgsql:host=localhost;port=5432;dbname=testdb;";
//Or even SQLite
//$dsn = "sqlite:/path/to/database"
$username = "user";
$password = "pass";
$db = new PDO($dsn, $username, $password);
// setup PDO to throw an exception if an invalid query is provided
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Next, let's prepare a statement for execution, with a single placeholder
$query = "SELECT * FROM users WHERE class = ?";
$statement = $db->prepare($query);
// Create some parameters to fill the placeholders, and execute the statement
$parameters = [ "221B" ];
$statement->execute($parameters);
// Now, loop through each record as an associative array
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
do_stuff($row);
}
該 prepare
函式建立從查詢字串 PDOStatement
物件。在此返回的物件上執行查詢和檢索結果。如果發生故障,該功能要麼返回 false
,要麼丟擲一個 exception
(取決於 PDO 連線的配置方式)。