MSSQL服务器未在codeigniter中配置。即没有建立连接

时间:2015-03-09 05:28:56

标签: php sql-server database codeigniter sqlsrv

我在使用数据库的codeigniter中建立连接时遇到问题。我已经尝试了很多并测试了不同人给出的许多解决方案,但到目前为止还没有成功。

我在codeigniter中的配置是:

$db['default']['hostname'] = 'DX-PC\MSSQLSERVER';
//$db['default']['port'] = 1433;
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypass';
$db['default']['database'] = 'mydbname';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE; //default: 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;

所以,它给了我以下错误:

  

发生数据库错误   无法使用提供的设置连接到数据库服务器。

同样,我使用以下代码尝试与MS SQL服务器建立连接,而不使用codeigniter:

    echo 'called';

    echo "<br>";

    $serverName = "DX-PC\\MSSQLSERVER"; //serverName\instanceName
    $connectionInfo = array( "Database"=>"mydbname", "UID"=>"myusername", "PWD"=>"mypass");
    $conn = sqlsrv_connect( $serverName,$connectionInfo);
    if( $conn ) {
        echo "Connection established.<br />";
    }else{
        echo "Connection could not be established.<br />";
        echo "<pre>";
        print_r(sqlsrv_errors());
        echo "</pre>";
    }

它给了我这些错误:

called Connection could not be established.

Array
  (
    [0] => Array
       (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 87
        [code] => 87
        [2] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. 
        [message] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. 
    )

[1] => Array
    (
        [0] => HYT00
        [SQLSTATE] => HYT00
        [1] => 0
        [code] => 0
        [2] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
        [message] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
    )

[2] => Array
    (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 87
        [code] => 87
        [2] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
        [message] => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
    )

我尝试过来自不同论坛和stackoverflow的每个解决方案,但没有成功。请帮助我。感谢

2 个答案:

答案 0 :(得分:0)

过去我也有问题,所以我用经典的方式做到了:

$server = "x.x.x.x\MSSQL2005,1435";
$user = "xxxx";
$pass = "xxxx";

$this->ms  = mssql_connect($server, $user, $pass) or die ("No connection");

答案 1 :(得分:0)

是的,我已经弄明白并且现在正在工作。 以下更改使其正常工作。

  1. 以前必须完成的配置。 ie(扩展名:sqlsrv _ * .dll等在php.ini中添加等)

  2. 将.htaccess中的重写规则从RewriteRule ^(。)$ /index.php?/$1 [L]更改为RewriteRule ^(。)$ /yourrootfolder / index.php ?/ $ 1 [L](礼貌SHAZ)

  3. 其次,因为我有一个名为MSSQLSERVER的默认实例,所以它不需要写$ db [&#39;默认&#39;] [&#39;主机名&#39;] =& #39; DX-PC \ MSSQLSERVER&#39 ;;而是WROTE $ db [&#39;默认&#39;] [&#39;主机名&#39;] =&#39; DX-PC&#39;;

  4. 我希望这可以帮助别人。感谢。