php 如何在 CodeIgniter 中连接到 SQL Server 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9973022/
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 08:00:40  来源:igfitidea点击:

How do I connect to a SQL Server database in CodeIgniter?

phpsql-server-2008codeigniterconfigurationconnection

提问by Bhavin Rana

How do I connect to a SQL Server database in CodeIgniter?

如何在 CodeIgniter 中连接到 SQL Server 数据库?

I'm currently starting an application in CodeIgniter and I would like to use SQL Server.

我目前正在 CodeIgniter 中启动一个应用程序,我想使用 SQL Server。

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = '#.#.#.27';
$db['default']['username'] = '@@@@@@';
$db['default']['password'] = '@@@@@@@@@';
$db['default']['database'] = '$$$$$$$$$$$$$';
$db['default']['dbdriver'] = 'mssql';
$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;

After auto-loading the database, it shows just a blank white page with no errors. Can you please tell me what other changes I have to make to work with the SQL Server database?

自动加载数据库后,它只显示一个没有错误的空白页面。你能告诉我使用 SQL Server 数据库还需要做哪些其他更改吗?

#autoload.php#
$autoload['libraries'] = array('database');

回答by heriawan

use use $db['default']['dbdriver'] = 'sqlsrv';
and install Microsoft Drivers for PHP for SQL Server on windows

使用使用 $db['default']['dbdriver'] = 'sqlsrv';
并在 Windows 上为 SQL Server 安装 Microsoft Drivers for PHP

CodeIgniter MSSQL connection

CodeIgniter MSSQL 连接

回答by Charity Leschinski

I couldn't get it to work with the supporteddriver of mssqlso I used sqlsrv

我无法让它与支持的驱动程序一起使用,mssql所以我使用了sqlsrv

I normally connect with ip,portas the hostname, so I changed that.

我通常使用ip,port作为连接hostname,所以我改变了它。

pconnectwas also giving me issues. I set it to FALSEand it started working.

pconnect也给我带来了问题。我将其设置为FALSE并开始工作。

Here is the configuration I got to work:

这是我开始工作的配置:

$db['default']['hostname'] = '127.0.0.1,1433';
$db['default']['username'] = 'username1';
$db['default']['password'] = 'secretpassword';
$db['default']['database'] = 'databasename';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

回答by PC.

I just got this problem solved. And I was connecting to MSSQL hosted with microsoft Azure

我刚刚解决了这个问题。我正在连接到微软 Azure 托管的 MSSQL

Steps I followed after several research work on internet are as follows :

在互联网上进行了几次研究工作后,我遵循的步骤如下:

database.cfg :

数据库.cfg:

$db['default']['hostname'] = 'XXXXXXX.database.windows.net';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'databasename';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;

Mainly dbdriver,pcconnect and the hostname you should configure properly. Rest are common. Get hostname from your azure database details.

主要是 dbdriver、pcconnect 和您应该正确配置的主机名。休息很常见。从 azure 数据库详细信息中获取主机名。

And I also modified couple of system files as I heard there were issues with DB driver.

我还修改了几个系统文件,因为我听说 DB 驱动程序有问题。

system/database/drivers/sqlsrv/sqlsrv_driver.php

系统/数据库/驱动程序/sqlsrv/sqlsrv_driver.php

function db_pconnect()
    {
        //$this->db_connect(TRUE);
        return $this->db_connect(TRUE);
    }

and

function affected_rows()
{
    //return @sqlrv_rows_affected($this->conn_id);
    return @sqlsrv_num_rows($this->result_id);
}

I was able to connect to database and create database application.

我能够连接到数据库并创建数据库应用程序。

Hope it will help someone in need :)

希望能帮到有需要的人 :)

回答by Martin Pfeffer

Follow these simple steps:

请按照以下简单步骤操作:

  1. Install the instance with "mixed mode"-enabled. If your unsure watch this- video starts at the right sequence. Maybe it's a good idea to watch it completely.

  2. change the db-config in '...ci/application/config/database.php'

    $active_group = 'default';
    $query_builder = TRUE;
    
    $db['default'] = array(
        'dsn' => '',
        'hostname' => 'localhost',
        'username' => 'sa', // <- use 'sa'
        'password' => 'THE_PASSWORD_YOU_SET_ON_INSTALL', // "mixed-mode"
        'database' => 'ci',
        'dbdriver' => 'sqlsrv',
    //  'dbdriver' => 'mysqli', // <- my old "non-server" config (approx. 70% slower)
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
    
  1. 安装启用了“混合模式”的实例。如果您不确定观看视频 - 视频以正确的顺序开始。也许完整地观看它是个好主意。

  2. 更改 ' ...ci/application/config/database.php' 中的 db-config

    $active_group = 'default';
    $query_builder = TRUE;
    
    $db['default'] = array(
        'dsn' => '',
        'hostname' => 'localhost',
        'username' => 'sa', // <- use 'sa'
        'password' => 'THE_PASSWORD_YOU_SET_ON_INSTALL', // "mixed-mode"
        'database' => 'ci',
        'dbdriver' => 'sqlsrv',
    //  'dbdriver' => 'mysqli', // <- my old "non-server" config (approx. 70% slower)
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
    

enter image description here

在此处输入图片说明

回答by FAISAL

it is the same process as mysql. use the same configuration given below:

它与mysql的过程相同。使用下面给出的相同配置:

$active_group = 'default'; $active_record = TRUE;

$db['default']['hostname'] = 'xxx.xx.xx.xx\SQLEXPRESS';

$db['default']['username'] = 'sa';

$db['default']['password'] =

'xxxxxxx'; $db['default']['database'] = 'xxxxx';

$db['default']['dbdriver'] = 'mssql';

$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;

$active_group = '默认'; $active_record = TRUE;

$db['default']['hostname'] = 'xxx.xx.xx.xx\SQLEXPRESS';

$db['default']['username'] = 'sa';

$db['default']['password'] =

'xxxxxx'; $db['default']['database'] = 'xxxxx';

$db['default']['dbdriver'] = 'mssql';

$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 your Model use same as

在您的模型中使用与

function selectagent($table,$agent) {

$field = $this->db->get_where($table, array('var1' =>$agent,'day(date)'=>date('d')));

  $fields=$field->result();

  return $fields;
}

功能选择代理($table,$agent){

$field = $this->db->get_where($table, array('var1' =>$agent,'day(date)'=>date('d')));

  $fields=$field->result();

  return $fields;
}

回答by qwertzman

How about this? I've seen it work on some servers. Is this a Windows server?

这个怎么样?我已经看到它在一些服务器上工作。这是 Windows 服务器吗?

$db['default']['dbdriver'] = "odbc";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE; 

further reading

进一步阅读

hope this helps

希望这可以帮助

回答by Starx

Your Configuration code is fine. Further reading here

您的配置代码很好。进一步阅读这里

IF you believe the errors are not showing up, then go to the index.phpand on the top place the following snippet to show the errors.

如果您认为错误没有出现,请转到index.php并在顶部放置以下代码段以显示错误。

error_reporting(E_ALL);

Other, then this check if the MSSQL service is running and is accessible. May be create a simple .phpand try a connection with plain codes.

其他,然后检查 MSSQL 服务是否正在运行并且可以访问。可以创建一个简单的.php并尝试使用纯代码连接。

A non CI file Something like,

一个非 CI 文件 类似的东西,

<?php
$server = 'YOURPC\SQLEXPRESS';
$link = mssql_connect($server, 'user', 'pass');

if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
?>