php CodeIgniter MSSQL 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8669337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:13:55  来源:igfitidea点击:

CodeIgniter MSSQL connection

phpsqlsql-servercodeigniterodbc

提问by Petre P?tra?c

I'm working on a web project that is to be deployed on a 64bit Windows 2008 Server machine running IIS 7.5 and PHP 5.3.8. The database on the system is Microsoft SQL Server 2008 R2. I'm developing the application on the CodeIgniter 2.1.0 framework, and I'm a bit stumped on getting it to connect to the SQL Server.

我正在开发一个 Web 项目,该项目将部署在运行 IIS 7.5 和 PHP 5.3.8 的 64 位 Windows 2008 Server 机器上。系统上的数据库是 Microsoft SQL Server 2008 R2。我正在 CodeIgniter 2.1.0 框架上开发应用程序,但我有点难以让它连接到 SQL Server。

I've tried using both the MSSQL, ODBC and SQLSRV database drivers, and have encountered three separate errors, for each different driver.

我已经尝试使用 MSSQL、ODBC 和 SQLSRV 数据库驱动程序,并且针对每个不同的驱动程序遇到了三个不同的错误。

Here is my configuration for ODBC:

这是我的 ODBC 配置:

$db['default']['hostname'] = 'SA';
$db['default']['username'] = 'petre';
$db['default']['password'] = 'start';
$db['default']['database'] = 'petre';
$db['default']['dbdriver'] = 'odbc';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

I have the DNS for SA defined under Data Sources, and I'm sure that the username and password are both valid, and that the server accepts Mixed authentication(Windows + SQL authentication).

我在数据源下定义了 SA 的 DNS,我确定用户名和密码都有效,并且服务器接受混合身份验证(Windows + SQL 身份验证)。

I'm auto-loading the DB library and for any page I access I get:

我正在自动加载数据库库,对于我访问的任何页面,我都会得到:

Unable to connect to your database server using the provided settings.
Filename: C:\inetpub\wwwroot\system\database\DB_driver.php
Line Number: 124

If I try to connect via MSSQL, I just get a blank page, no matter what.

如果我尝试通过 MSSQL 连接,无论如何我都会得到一个空白页面。

If I try using SQLSRV using the following params:

如果我尝试使用以下参数使用 SQLSRV:

$db['default']['hostname'] = '127.0.0.1';
$db['default']['username'] = 'petre';
$db['default']['password'] = 'start';
$db['default']['database'] = 'petre';
$db['default']['dbdriver'] = 'sqlsrv';

Even though I'm linking to the sqlsrv DLL file in php.ini, I get the following message:

即使我链接到 php.ini 中的 sqlsrv DLL 文件,我也会收到以下消息:

PHP Fatal error:  Call to undefined function sqlsrv_connect() in C:\inetpub\wwwroot\system\database\drivers\sqlsrv\sqlsrv_driver.php on line 76

I've been reading up on old posts and different views but I've yet to solve the problem.

我一直在阅读旧帖子和不同的观点,但我还没有解决这个问题。

I'm only looking for a solution for a single DB Driver - anything that gets it to connect is fine. Does anyone have any advice on solving this?

我只是在为单个数据库驱动程序寻找解决方案 - 任何让它连接的东西都很好。有没有人对解决这个问题有任何建议?

采纳答案by kop48

The error you are showing for the SQLSRV driver implies the DLL is never loading. What does your php_info() say?

您为 SQLSRV 驱动程序显示的错误意味着 DLL 永远不会加载。你的 php_info() 怎么说?

Also, do you have the SQL Server Native Access Client (SNAC) 10 installed? The SQLSRV driver requires it.

另外,您是否安装了 SQL Server Native Access Client (SNAC) 10?SQLSRV 驱动程序需要它。

回答by heriawan

For x86 windownload http://www.microsoft.com/en-us/download/details.aspx?id=20098for using with:

对于x86 win下载http://www.microsoft.com/en-us/download/details.aspx?id=20098使用:

$db['default']['dbdriver'] = 'sqlsrv';
use the php_sqlsrv_XX_ts_vcX.dll in php.ini extension

if your webserver and your database server are not on same server with your app, install this:

如果您的网络服务器和数据库服务器与您的应用程序不在同一台服务器上,请安装:

http://go.microsoft.com/fwlink/?LinkID=188400&clcid=0x409

and for linuxas webserver:
$db['default']['dbdriver'] = 'mssql';

对于linux作为网络服务器:
$db['default']['dbdriver'] = 'mssql';

download & install = php5-sybase + freetds
<br/>edit /usr/local/etc/freetds.conf
<br/>create pdo_dblib.so & php_mssql.so (if not allready exist on your extension dir), and apply to php.ini

sudo /etc/init.d/apache2 restart

and just dont forget to allow connection to your DB (default port for mssql is 1433)

并且不要忘记允许连接到您的数据库(mssql 的默认端口是 1433)