Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Creates a connection to a SQL Server database.
Syntax
PDO::__construct($dsn [,$username [,$password [,$driver_options ]]] )
Parameters
$dsn: A string that contains the prefix name (always sqlsrv), a colon, and the Server keyword. For example, "sqlsrv:server=(local)". You can optionally specify other connection keywords. See Connection Options for a description of the Server keyword and the other connection keywords. The entire $dsn is in quotation marks, so each connection keyword should not be individually quoted.
$username: Optional. A string that contains the user's name. To connect using SQL Server Authentication, specify the login ID. To connect using Windows Authentication, specify "".
$password: Optional. A string that contains the user's password. To connect using SQL Server Authentication, specify the password. To connect using Windows Authentication, specify "".
$driver_options: Optional. You can specify PDO Driver Manager attributes, and Microsoft Drivers for PHP for SQL Server specific driver attributes -- PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ATTR_DIRECT_QUERY. When PDO::ATTR_ERRMODE is PDO::ERRMODE_EXCEPTION, the default, an invalid attribute or attribute value makes the constructor throw a PDOException with SQLSTATE IMSSP. PDO::setAttribute validates attributes the same way.
Set PDO::SQLSRV_ATTR_DEFAULT_STR_LENGTH in $driver_options to specify the initial adaptive length for eligible input strings in new statements. The default is 0 (disabled); accepted values are integers from 0 through 8,000. An invalid value reports SQLSTATE IMSSP. For examples and limitations, see Declare input string types and lengths (PDO_SQLSRV).
Return Value
Returns a PDO object. If failure, returns a PDOException object.
Exceptions
PDOException
Remarks
You can close a connection object by setting the instance to null.
After a connection, PDO::errorCode displays 01000 instead of 00000.
If PDO::__construct fails for any reason, an exception is thrown, even if PDO::ATTR_ERRMODE is set to PDO::ERRMODE_SILENT.
Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.
Example with database
This example shows how to connect to a server using Windows Authentication, and specify a database.
<?php
$c = new PDO( "sqlsrv:Server=(local) ; Database = AdventureWorks ", "", "", array(PDO::SQLSRV_ATTR_DIRECT_QUERY => true));
$query = 'SELECT * FROM Person.ContactType';
$stmt = $c->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
print_r( $row );
}
$c = null;
?>
Example without database
This example shows how to connect to a server, specifying the database later.
<?php
$c = new PDO( "sqlsrv:server=(local)");
$c->exec( "USE AdventureWorks2022" );
$query = 'SELECT * FROM Person.ContactType';
$stmt = $c->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row );
}
$c = null;
?>