PDO::__construct
PDO::__construct — Creates a PDO instance representing a database connection (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
Description
Syntax
PDO::__construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )
Creates a PDO instance representing a database connection to the requested database.
Parameters
- dsn: Data source name, or DSN, contains the information required to connect to the database.
- username: Username in the DSN string. This parameter is optional for some PDO drivers.
- password: Password in the DSN string. This parameter is optional for some PDO drivers.
- driver_options: A key=>value array of driver-specific connection options.
Return Values
Returns a PDO object on success.Errors/Exceptions
If the attempt to connect to the requested database fails, PDO::__construct() throws a PDOException.
Examples
Create a PDO instance via driver invocation
<?php
/* 通过调用驱动程序创建一个PDO实例 */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Other Extensions
PHP PDO Reference Manual