男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站

PDO::__construct

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)

PDO::__construct 創(chuàng )建一個(gè)表示數據庫連接的 PDO 實(shí)例

說(shuō)明

PDO::__construct(
    string $dsn,
    string $username = ?,
    string $password = ?,
    array $driver_options = ?
)

創(chuàng )建一個(gè)表示連接到請求數據庫的數據庫連接 PDO 實(shí)例。

參數

dsn

數據源名稱(chēng)或叫做 DSN,包含了請求連接到數據庫的信息。

通常,一個(gè) DSN 由 PDO 驅動(dòng)名、緊隨其后的冒號、以及具體 PDO 驅動(dòng)的連接語(yǔ)法組成。更深入的信息能從 PDO 具體驅動(dòng)文檔找到。

The dsn 參數支持三種不同的方式 創(chuàng )建一個(gè)數據庫連接:

Driver invocation

dsn 包含完整的DSN。

URI invocation

dsn consists of uri: followed by a URI that defines the location of a file containing the DSN string. The URI can specify a local file or a remote URL.

uri:file:///path/to/dsnfile

Aliasing

dsn consists of a name name that maps to pdo.dsn.name in php.ini defining the DSN string.

注意:

別名必須得在 php.ini 中定義了,不能是在 .htaccesshttpd.conf 中 。

username

DSN字符串中的用戶(hù)名。對于某些PDO驅動(dòng),此參數為可選項。

password

DSN字符串中的密碼。對于某些PDO驅動(dòng),此參數為可選項。

driver_options

一個(gè)具體驅動(dòng)的連接選項的鍵=>值數組。

返回值

成功則返回一個(gè)PDO對象。

錯誤/異常

如果試圖連接到請求的數據庫失敗,則PDO::__construct() 拋出一個(gè) PDO異常(PDOException) 。

范例

示例 #1 Create a PDO instance via driver invocation

<?php
/* Connect to an ODBC database using driver invocation */
$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();
}

?>

示例 #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
/* Connect to an ODBC database using driver invocation */
$dsn 'uri:file:///usr/local/dbconnect';
$user '';
$password '';

try {
    
$dbh = new PDO($dsn$user$password);
} catch (
PDOException $e) {
    echo 
'Connection failed: ' $e->getMessage();
}

?>

示例 #3 使用別名創(chuàng )建一個(gè)PDO實(shí)例

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
/*  使用別名連接到一個(gè)ODBC數據庫  */
$dsn 'mydb';
$user '';
$password '';

try {
    
$dbh = new PDO($dsn$user$password);
} catch (
PDOException $e) {
    echo 
'Connection failed: ' $e->getMessage();
}

?>

男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站