【问题标题】:MSSQL server not configuring in codeigniter. i.e. not establishing a connectionMSSQL 服务器未在 codeigniter 中配置。即不建立连接
【发布时间】:2015-03-09 05:28:56
【问题描述】:

我在使用 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;

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

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

同样,我已经尝试使用以下代码在不使用 codeigniter 的情况下与 MS SQL 服务器建立连接:

    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 的所有解决方案,但没有成功。请帮助我。谢谢

【问题讨论】:

  • 在 .htaccess 文件中,检查或更正您的网址:RewriteRule ^(.*)$ /yourRootFolder/index.php?/$1 [L]
  • RewriteEngine On RewriteBase /cheddargetter/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /cheddargetter/index.php? /$1 [L] 我已经改变了它。仍然出现 db 错误

标签: php sql-server database codeigniter sqlsrv


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    是的,我已经想通了,它现在正在工作。 以下更改使其正常工作。

    1. 之前的配置,您必须在之前完成。即(扩展名:sqlsrv_*.dll 等添加到 php.ini 等)

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

    3. 其次,因为我有一个名为 MSSQLSERVER 的默认实例,所以它不需要写 $db['default']['hostname'] = 'DX-PC\MSSQLSERVER';而是写了 $db['default']['hostname'] = 'DX-PC';

    我希望这可以帮助其他人。谢谢。

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 2021-06-01
      • 2023-03-19
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 2010-11-26
      • 2020-10-08
      • 1970-01-01
      相关资源
      最近更新 更多