|
PHP5中文手册
PDO->__construct()(PHP 5 >= 5.1.0, PECL pdo:0.1-1.0.3) PDO->__construct() — Creates a PDO instance representing a connection to a database 说明PDO
PDO __construct
( string $dsn
[, string $username
[, string $password
[, array $driver_options
]]] )
Creates a PDO instance to represent a connection to the requested database. 参数
返回值Returns a PDO object on success. 错误/异常PDO->__construct() throws a PDOException if the attempt to connect to the requested database fails. 范例
Example#1 Create a PDO instance via driver invocation
<?php Example#2 Create a PDO instance via URI invocation The following example assumes that the file /usr/local/dbconnect exists with file permissions that enable PHP to read the file. The file contains the PDO DSN to connect to a DB2 database through the PDO_ODBC driver:
The PHP script can then create a database connection by simply passing the uri: parameter and pointing to the file URI:
<?php Example#3 Create a PDO instance using an alias The following example assumes that php.ini contains the following entry to enable a connection to a MySQL database using only the alias mydb: [PDO] pdo.dsn.mydb="mysql:dbname=testdb;host=localhost"
<?php |