|
PDO::__constructCreates a PDO instance representing a connection to a database Description
public PDO::__construct
( string
$dsn
[, string $username
[, string $password
[, array $driver_options
]]] )Creates a PDO instance to represent a connection to the requested database. Parameters
Return ValuesReturns a PDO object on success. Errors/ExceptionsPDO::__construct throws a PDOException if the attempt to connect to the requested database fails. Examples
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: odbc:DSN=SAMPLE;UID=john;PWD=mypass 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
|