Saturday, 10 September 2016

How to interact with mssql server through php?

To interact with SQL server in WINDOWS, its based on php version, need to place either mssql.dll or sqlsrv dll's.  This is stated clearly in . In UNIX / LINUX, need to install the FreeTDS library and use mssql_connect.
Now, use sqlsrv_connect/mssql_connect to connect to server, sqlsrv_query / mssql_query to run a query over the server, sqlsrv_fetch_array / mssql_fetch_array to fetch the data if needed.
$server = "";
$database = "testDb";
$UID = "user";
$PWD = "password";
$sql_query = "EXEC test.Get_Details";
/* For sqlsrv: */
$connection = sqlsrv_connect($server,array( "Database"=>$database,"UID"=>$UID,"PWD"=>$PWD));
$msqldbobj = sqlsrv_query( $connection, $sql_query);
while( $row = sqlsrv_fetch_array( $msqldbobj, SQLSRV_FETCH_ASSOC) ) {
/* For mssql: */
else if(function_exists('mssql_connect')){
$connection = mssql_connect($server,$UID,$PWD);
mssql_select_db($database, $connection);
$msqldbobj = mssql_query($sql_query);
while( $row = mssql_fetch_array($msqldbobj, MSSQL_ASSOC) ) {
echo 'server drivers not available';

No comments:

Post a Comment

Your comment is so valuable as it would help me in my growth of knowledge.