PDO :: __ construct
PDO :: __ construct - Creates a PDO instance database connection (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)
Explanation
grammar
PDO::__construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )
Creates a request to connect to the database connection to the database PDO instance.
Parameter Description
- dsn: Data Source Name or so-called DSN, contains the information required to connect to the database.
- username: DSN string username. For some PDO drivers, this parameter is optional.
- password: DSN string password. For some PDO drivers, this parameter is optional.
- driver_options: a key driver specific connection options => value array.
return value
Returns a PDO object on success.Error / exception
If you try to connect to the requested database fails, the PDO :: __ construct () throws an exception PDO (PDOException).
Examples
Create a PDO instance by calling the driver
<?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(); } ?>