Connecting to MSSQL from PHP in Linux

Installing FreeTDS

wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
./configure \
–prefix=/usr/local/freetds \
–enable-msdblib
make
make install

Configuring php

./configure \
–with-apxs2=/usr/local/apache/bin/apxs \
–with-openssl \
–enable-track-vars \
–with-mysql=/usr/local/mysql/ \
–with-mssql=/usr/local/freetds

Sample Code

$s = “server:1433”;
$u = “sa”;
$p = “password”;

$msconnect=mssql_connect ($s,$u,$p);
$msdb=mssql_select_db(“Northwind”,$msconnect);
$msquery = “select titleofcourtesy,firstname,lastname from employees”;
$msresults= mssql_query($msquery);
$msconnect=mssql_connect ($s,$u,$p);
$msdb=mssql_select_db(“Northwind”,$msconnect);
$msquery = “select titleofcourtesy,firstname,lastname from employees”;
$msresults= mssql_query($msquery);
while ($row = mssql_fetch_array($msresults)) {
echo $row[‘titleofcourtesy’] .” “. $row[‘firstname’] .” “. $row[‘lastname’] ;
}

Leave a Reply