php 在 Xampp 中连接 sqlsrv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22831500/
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
Connect sqlsrv in Xampp
提问by Marten
I have installed Xampp with a CodeIgniter installation. I want to connect from CodeIgniter to a SQL database.
我已经通过 CodeIgniter 安装安装了 Xampp。我想从 CodeIgniter 连接到 SQL 数据库。
I changed the database config file and set the dbdriver to sqlsrv.
我更改了数据库配置文件并将 dbdriver 设置为 sqlsrv。
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'IP Adress;
$db['default']['username'] = 'DBUserName';
$db['default']['password'] = 'DBPassword';
$db['default']['database'] = 'DBName';
$db['default']['dbdriver'] = 'sqlsrv';
$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;
In my controller I have the following code to try the connection:
在我的控制器中,我有以下代码来尝试连接:
$this->load->database();
$db_obj = $this->db->load('sql_Test',TRUE);
$connected = $db_obj->initialize();
if (!$connected){
$db_obj = $this->d->load('yyy',TRUE);
}
else{
die('connected');
}
I have the following error:
我有以下错误:
Fatal error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\system\database\drivers\sqlsrv\sqlsrv_driver.php on line 76
致命错误:在第 76 行调用 C:\xampp\htdocs\system\database\drivers\sqlsrv\sqlsrv_driver.php 中未定义的函数 sqlsrv_connect()
I have read on a forum that I have to change line 89 from sqlsrv_driver.php:
我在一个论坛上读到我必须从 sqlsrv_driver.php 更改第 89 行:
function db_pconnect()
{
// $this->db_connect(TRUE); original
return $this->db_connect(TRUE);
}
What do I wrong?
我怎么了?
回答by sunny
EDIT- First you need to download the driver http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
编辑 - 首先你需要下载驱动程序 http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
Now go to your XAMPP installation and search for php.dll It will display correct PHP dll you have.
现在转到您的 XAMPP 安装并搜索 php.dll 它将显示您拥有的正确 PHP dll。
1) move following files to xampp/php/ext directory.
1) 将以下文件移动到 xampp/php/ext 目录。
php_sqlsrv_53_nts_vc9.dll
php_pdo_sqlsrv_53_nts_vc9.dll
2) If you have php5ts.dll then move following files to xampp/php/ext directory.
2) 如果您有 php5ts.dll,则将以下文件移动到 xampp/php/ext 目录。
php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll
above files should be used if your PHP version is compiled with Visual C++ 9.0 . Else following files should be used.
如果您的 PHP 版本是使用 Visual C++ 9.0 编译的,则应使用上述文件。否则应使用以下文件。
1) If you have php.dll then move following files to xampp/php/ext directory.
1) 如果您有 php.dll,则将以下文件移动到 xampp/php/ext 目录。
php_sqlsrv_53_nts_vc6.dll
php_pdo_sqlsrv_53_nts_vc6.dll
2) If you have php5ts.dll then move following files to xampp/php/ext directory.
2) 如果您有 php5ts.dll,则将以下文件移动到 xampp/php/ext 目录。
php_sqlsrv_53_ts_vc6.dll
php_pdo_sqlsrv_53_ts_vc6.dll
Now we have to load files that we added recently. Open the php ini file and add entry in the area of dynamic extensions as follow.
现在我们必须加载我们最近添加的文件。打开php ini文件,在动态扩展区添加如下条目。
extension=php_sqlsrv_53_nts_vc9.dll
extension= php_pdo_sqlsrv_53_nts_vc9 .dll
Save the ini files and restart XAMPP
保存 ini 文件并重启 XAMPP
$check= @$CI->load->database($config, TRUE); // ommit the error
if ($check->call_function('error') !== 0) {
// Failed to connect
}
I don't know for sure what you are trying to do but in codeigniter you don't need to initialise database, CI automatically does it for you
我不确定您要做什么,但在 codeigniter 中您不需要初始化数据库,CI 会自动为您完成
so-
所以-
$this->load->database();
$db_obj = $this->db->load('SQL_Test',TRUE);
$connected = $db_obj->initialize();
this is not needed.
这是不需要的。
You just need to load the model and in model start performing queries. $this->load->database(); In controller you need to load the model like-
您只需要加载模型并在模型中开始执行查询。$this->load->database(); 在控制器中,您需要加载模型,例如-
$this->load->model('my_model');
then call the model function in which you have written the queries.
然后调用您在其中编写查询的模型函数。
$this->my_model->myfunction();
回答by Gerben Jacobs
Your XAMPP setup doesn't have sqlsrv support.
You need to enable it in php.ini
.
您的 XAMPP 设置不支持 sqlsrv。您需要在php.ini
.
extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll