PDO:: _ construct explanation
- 2021-11-13 06:53:59
- OfStack
PDO::_construct
PDO:: _ construct-Creates an PDO instance representing a database connection (PHP 5 > = 5.1.0, PECL pdo > = 0.1.0)
Description
Grammar
PDO::_construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )
Create a database connection PDO instance that represents a connection to the request database.
Parameter description
dsn: The data source name, or DSN, contains information for requesting a connection to the database. username: The user name in the DSN string. This parameter is optional for some PDO drivers. password: The password in the DSN string. This parameter is optional for some PDO drivers. driver_options: Key for 1 specific driver connection option = > An array of values.Return value
Success returns 1 PDO object.
Error/exception
If an attempt to connect to the requested database fails, the
PDO::__construct()
Throws an PDO exception (PDOException).
Instances
Create 1 PDO instance by calling the driver
<?php
/* Created by calling the driver 1 A PDO Instances */
$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();
}
?>
Summarize