我在Linux Debian 6 x64上有Apache 2.2.16和
PHP 5.4.3.
我以这种方式配置了我的odbc.ini文件:
[mydsn]
Driver = SQL Server Native Client 11.0
Database = datbase
Server = xxx.xxx.xxx.xxx,port
这样我的odbcinst.ini:
[SQL Server Native Client 11.0]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
Threading=1
UsageCount=1
要测试,我运行以下命令:
$isql -v mydsn dbusername dbpassword
我成功了
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
然后,使用phpize在PHP 5.4上安装unixODBC,使用:
(使用第一个命令ln -s …,因为./configure在默认位置找不到php的头文件)
$sudo ln -s /usr/include/php5 /usr/include/php
$phpize
$./configure --with-pdo-odbc=unixODBC && make && make test
$sudo make install
在我的phpinfo()我得到:
PDO support - enabled
PDO drivers - odbc
PDO Driver for ODBC (unixODBC) - enabled
ODBC Connection Pooling - Enabled, strict matching
现在是时候用PHP 5.4脚本来测试所有内容了
ini_set('display_errors', 1);
error_reporting(E_ALL);
$conn = new PDO('odbc:DSN=mydsn;UID='.$usr.';PWD='.$psw);
$query = 'select * from my_table';
$stmt = $conn->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch()) {
echo "
";
print_r($row);
echo "
";}
?>
但它不工作…我收到这个错误信息:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[01000] SQLDriverConnect: 0
[unixODBC][Driver Manager]Can't open lib '/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0' : file not found'
in /var/www/testemssql.php:17
Stack trace:
#0 /var/www/testemssql.php(17): PDO->__construct('odbc:DSN=mydsn...')
#1 {main} thrown in /var/www/testemssql.php on line 17
所以我的问题是:发生了什么?我缺少什么配置?
如何在Linux和PHP 5.4上正确设置MSSQL Server的Native ODBC驱动程序?
Ps .:当我尝试使用odbc_connect()PHP说该函数不存在.