php ms sql server code example

Example 1: php connect ms sql server

<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

Example 2: mssql php

$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");

$hostname may need to be configured as either...
$hostname.':'.$port;

Tags:

Misc Example