php tutorial SQL Server Authentication connection part code
*/
$serverName = "(local)"; //Database tutorial server address
$uid = "pandao"; //Database username
$pwd = "1987"; //Database password
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false)
{
echo "Connection failed!";
Die( print_r( sqlsrv_errors(), true));
}
$query = sqlsrv_query($conn, "SELECT TOP 10 nid,title,content FROM test.dbo.news");
while($row = sqlsrv_fetch_array($query)){
echo $row['nid']."-----".$row['title']."
";
}
?>
sqlserver2005 or sqlserver2008
Please download the driver here first
http://www.microsoft.com/downloads/details.aspTutorialx?FamilyId=61BF87E0-D031-466B-B09A-6597C21A2E2A&displaylang=en
Unzip the file after downloading
Configuration:
1. Place the decompressed php_sqlsrv.dll and php_sqlsrv_ts.dll into the PHP extension directory (PHPEXT).
2. Edit the php.ini file (under the windows folder) and add the following extension:
extension=php_sqlsrv.dll
extension=php_sqlsrv_ts.dll
3. Remove the semicolon
There are two commonly used authentication methods in SQL Server. One is local system account authentication (Windows Authentication), and the other is using user name and password (SQL Server Authentication). The second authentication method must enable the mixed mode of SQL Server. .
1. Windows Authentication connection part code snippet:
$serverName = "(local)";
$connectionInfo = array("Database"=>"TestingInfo","ConnectionPooling"=>false);
$conn = sqlsrv_connect($serverName,$connectionInfo);
if(! $conn){
echo "o no!!!!!";
die( print_r( sqlsrv_errors(), true));
}else{
echo "yes done";
}?>
2.SQL Server Authentication connection part code snippet:
$serverName = "(local)";
$uid = "dbusername";//Database user name
$pwd = "dbuserpass";//Database user password
//The following Database is the database name
$connectionInfo = array("UID"=>$uid,"PWD"=>$pwd,"Database"=>"dbname");
$conn = sqlsrv_connect($serverName,$connectionInfo);
if(! $conn){
echo "o no!!!!!!!";
die( print_r( sqlsrv_errors(), true));
}else{
echo "yes done";
}
?>