MSSQL - PHP on Apache - Linux Redhat
When using php 5.2.10 please beaware of this error:
http://bugs.php.net/bug.php?id=42068
Standard odbc_connect will not work, you must use pdo_odbc
Connecting to MSSQL using pdo odbc - walkthrough..
1. Download and configure FreeTDS with-unixodbc
./configure --prefix=/opt/SYSfreetds --with-unixodbc
make;make test; make install
2. install php-odbc and unixODBC
php-odbc-5.2.10-1.x86_64.rpm
unixODBC.x86_64.x86x64
3. Setup ODBC links
a)
Create a tds.driver file with the following contents
[FreeTDS]
Description = v0.63 with protocol v8.0
Driver = /opt/SYSfreetds/lib/libtdsodbc.so
Register the ODBC driver - the tds.driver file
odbcinst -i -d -f tds.driver
b)
Creating a tds.datasource file - ODBC Data Source with contents:
[SOURCENAME]
Driver=FreeTDS
Description=Test MS SQL Database with FreeTDS
Trace=No
Server=BobTheServer
Port=1433
TDS Version=8.0
Database=youDBname
Register the ODBC data source
odbcinst -i -s -f tds.datasource
Beware that the odbc.ini file will be installed in the current users home directory. This may need to be used if you are using a webserver as the apache home directory could be different.
Ensure .odbc.ini is in apaches home directory, possibly "/var/www"
4. Test the ODBC link on the command line
isql -v SOURCENAME 'username' 'password'
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
5. Edit /etc/php.ini
Make sure the following is set:
mssql.secure_connection = On
6. Restart apache gracefully
7. PHP to run:
$dbh= new PDO('odbc:SOURCENAME', 'username', 'password');
$stmt = $dbh->prepare("$query");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
?>
Trouble-shooting:
Please try strace/ truss if you encounter issues. It could be you are referencing wrong libraries somewhere.
Ensure you have restarted apache once the odbc files are in place