2011-07-09

php 5.3.3 連 SQL Server 2008 or SQL Server 2012 範例

$host = "W2003\SQLEXPRESS";
$uid = "sa";
$pwd = "123";
$database = "tempdb";
try {
   //$conn = new PDO( "sqlsrv:Server=w2003\SQLEXPRESS;Database=obw", "sa", "123");
   $conn = new PDO( "sqlsrv:server=$host;Database = $database", $uid, $pwd);
   $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch( PDOException $e ) {
   die( "Error connecting to SQL Server" );
}
echo "Connected to SQL Server\n";
$query = 'select * from Person.ContactType';
$stmt = $conn->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
   print_r( $row );
}

php5.3 連接 SQL Server 2012 需要增加安裝「SQL Server 2008 R2 Native Client」
否則上面會連不上
下載網址:https://msdn.microsoft.com/en-us/library/cc296170.aspx

如果WINDOWS系統是x64 就下載 下圖 X64 package

沒有留言: