how can i build php with MS SQL support

angelf9

Verified User
Joined
Apr 15, 2008
Messages
16
is there anything that needs to be done except adding "--with mssql" in configure/configure.php5?
do i have to install a rpm or something?
any pointers will be greatly appreciated :)


Currently running Fedora 4 with:
Apache 2.0.63
PHP 5.2.6
MySQL 5.0.51a
Dovecot 1.1.3
 
resolved

After days of searching the web i found something related to this and took a leap of faith.
I'll post the steps i followed, maybe it helps someone.

1. Install unixODBC package via yum or the package manager of your OS. It has some libs required by FreeTDS (at least in my case).

2. Download FreeTDS
I used freetds-0.63.tar.gz (reason unknown :confused:) from http://www.ibiblio.org/pub/Linux/ALPHA/freetds/old/0.63/

Latest stable version can be found here http://www.ibiblio.org/pub/Linux/ALPHA/freetds/stable/ (0.82 at the time of this post)
(I also found an rpm but that didn't work for me.)

3. Configure and install FreeTDS
Uncompress and cd to the respective folder:

./configure --with-tdsver=4.2 --prefix=/usr/local/freetds
make
make install

4. PHP configuration
Edit the php configuration file.
"/usr/local/directadmin/custombuild/configure/ap2/configure.php5" in my case. (i use apache 2.0.63 and php 5.2.6)
Add this line: "--with mssql=/usr/local/freetds" (without quotes)

Go to custombuild:
./build clean
./build php n

All done. :)
This worked for me, but I think it will work on any system or configuration.
 
Last edited:
Because MSSQL is a Microsoft product. It doesn't work on either Linux or FreeBSD.

Jeff
 
As it was pointed out by Jeff, MSSQL server is a Microsoft product thus only works on Windows.
The guide i posted back in september provides you with the means to connect to a MSSQL server from php on a Fedora core 4 OS which is not supported anymore and yum did not work for me.

In the meantime I installed CentOS 5 and I made it work like this:
1. Install freetds:
Code:
yum install freetds

2. Edit the php configuration file (i have apache 2 and php 5, change acording to your configuration):
Code:
mkdir /usr/local/directadmin/custombuild/custom/ap2
cp /usr/local/directadmin/custombuild/configure/ap2/configure.php5 /usr/local/directadmin/custombuild/custom/ap2
edit and add this line to /usr/local/directadmin/custombuild/custom/ap2/configure.php5:
Code:
--with mssql=/usr \
(Note: if you write this at the end of the file make sure you remove "\" and add it to the line before it)

3. Rebuild php:
Code:
cd  /usr/local/directadmin/custombuild
./build clean
./build update
./build php n

Again, this does not install MSSQL server on your Linux host (that is only posible with Windows), but merely provides you with the means to connect to an external MSSQL database.
Hope this helps.
 
Thanks, limpangel. This is one of those cases where responding only to the message rather than reading the thread has made me look a bit of a fool :).

Jeff
 
Thanks, limpangel. This is one of those cases where responding only to the message rather than reading the thread has made me look a bit of a fool :).

Jeff

I was ready to respond but someone did it before :)
 
Code:
--with-mssql=/usr \
(Note: if you write this at the end of the file make sure you remove "\" and add it to the line before it)

3. Rebuild php:

I found a typo but it still doesn't works..
 
Last edited:
Well, manually installing latest FreeTDS (0.84)

Code:
./configure --prefix=/usr/local/freetds
make
make install

and adding

Code:
--with-mssql=/usr/local/freetds \

to /usr/local/directadmin/custombuild/custom/ap2/configure.php5
did the job.
 
Can anyone offer some connection string examples they used in PHP once getting this going? I am going to give this a shot but once done, not sure what's next in PHP. I am use to interfacing with MySQL only within PHP and I have done lots of work in the old days connecting to MS SQL from ASP. I am new to MS SQL from PHP so, any examples would be appreciated.

I see documentation and examples out there but I am not sure if they pertain to those that set it all up with TDS like this or some other method.

Do I just do something like this?
Code:
// Connect to the database (host, username, password)
$lnk = mssql_connect('localhost','UID','pass') 
    or die('Could not connect to the server!');
 
// Select a database:
mssql_select_db('dbname') 
    or die('Could not select a database.');
 
// Example query: (TOP 10 equal LIMIT 0,10 in MySQL)
//$SQL = "SELECT TOP 10 * FROM ExampleTable ORDER BY ID ASC";
$qryCust = "SELECT * FROM Customers";

// Execute query:
$qryRslt = mssql_query($qryCust) 
    or die('An error occured: ' . mysql_error());
 
// Get result count:
//$Count = mssql_num_rows($result);
//print "Showing $count rows:<hr/>\n\n";
 
// Fetch rows:
print "Starting loop";

while ($rowCust = mssql_fetch_assoc($qryRslt)) {
 
    print "Cust: ".$rowCust['Customer']."\n";
 
}
 
mssql_close($lnk);
 
@WholesaleDialup

Wrong forums. You might be luckier on specialized forums, search a link with help of Google.
 
I had to install freetds-devel too because it includes libsybdb.so and it was necessary for installation also I had to create a symbolic link as following line

ln -s /usr/lib64/libsybdb.so.5.0.0 /usr/lib/libsybdb.so
 
As it was pointed out by Jeff, MSSQL server is a Microsoft product thus only works on Windows.
The guide i posted back in september provides you with the means to connect to a MSSQL server from php on a Fedora core 4 OS which is not supported anymore and yum did not work for me.

In the meantime I installed CentOS 5 and I made it work like this:
1. Install freetds:
Code:
yum install freetds

2. Edit the php configuration file (i have apache 2 and php 5, change acording to your configuration):
Code:
mkdir /usr/local/directadmin/custombuild/custom/ap2
cp /usr/local/directadmin/custombuild/configure/ap2/configure.php5 /usr/local/directadmin/custombuild/custom/ap2
edit and add this line to /usr/local/directadmin/custombuild/custom/ap2/configure.php5:
Code:
--with mssql=/usr \
(Note: if you write this at the end of the file make sure you remove "" and add it to the line before it)

3. Rebuild php:
Code:
cd  /usr/local/directadmin/custombuild
./build clean
./build update
./build php n

Again, this does not install MSSQL server on your Linux host (that is only posible with Windows), but merely provides you with the means to connect to an external MSSQL database.
Hope this helps.
I had to install freetds-devel too because it includes libsybdb.so and it was necessary for installation also I had to create a symbolic link as following line

ln -s /usr/lib64/libsybdb.so.5.0.0 /usr/lib/libsybdb.so
 
Here is how it worked for me on centos 7 with php 5.4 ap2

yum install freetds unixodbc unixodbc-devel -y

add the following lines to /usr/local/directadmin/custombuild/custom/ap2/configure.php54

--with-pdo-dblib=/usr/local/freetds \
--with-pdo-odbc=unixODBC,/usr/ \

rebuild php with custombuild.

example connection string :


echo "<pre>", print_r(PDO::getAvailableDrivers()), "</pre>";
try {
$hostname = "[server_ip]";
$port = 1433;
$dbname = "[dbname]";
$username = "[username]";
$pw = "[pass]";
$dbh = new PDO ("dblib:version=7.0;charset=UTF-8;host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
 
Last edited:
Here is how it worked for me on centos 7 with php 5.4 ap2

yum install freetds unixodbc unixodbc-devel -y

add the following lines to /usr/local/directadmin/custombuild/custom/ap2/configure.php54

--with-pdo-dblib=/usr/local/freetds \
--with-pdo-odbc=unixODBC,/usr/ \

rebuild php with custombuild.

example connection string :


echo "<pre>", print_r(PDO::getAvailableDrivers()), "</pre>";
try {
$hostname = "[server_ip]";
$port = 1433;
$dbname = "[dbname]";
$username = "[username]";
$pw = "[pass]";
$dbh = new PDO ("dblib:version=7.0;charset=UTF-8;host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
UH a online server with PHP 5x ? even worse 5.4x :eek:

for php 5.6x there are backported security updates for some repo's

i mean my reply well...

pdo is better ok... yup
 
There are 10 year old sites on the server :)

UH a online server with PHP 5x ? even worse 5.4x :eek:

for php 5.6x there are backported security updates for some repo's

i mean my reply well...

pdo is better ok... yup
 
Back
Top