Connect PHP to SQL Server

Check out how to make a connection between PHP and MS SQLServer.

< ?php
$conn = mssql_connect('Server\Instance', 'user', 'passwd'); 
if(!$conn) die('Couldn\'t connect to DB');
$sqldb = mssql_select_db('myDB', $conn);
 
$sql = 'select columnDB from table';
$res = mssql_query($sql);
if(mssql_num_rows($res) == 0) {
   // no rows found
   $columnValue = '';
} else {
   $columnValue = mssql_result($res, 0, 'columnDB');
}
mssql_close($conn);
?>

The most common problem I found at blogs and forums is the one related to the correct version of sqlserver lib to use. You can download the one I use: ntwdblib

Attention: don’t forget to uncomment the php extension for MS SQL Server functions in the php.ini file.
The line
;extension=php_mssql.dll
should be modified to
extension=php_mssql.dll

by LSimpson

Leave a Reply

You must be logged in to post a comment.