使用Codeigniter连接到(Linux)Centos上的MS SQL

时间:2014-04-15 13:14:58

标签: php sql sql-server linux codeigniter

我喜欢在(Linux)Centos上使用Codeigniter连接MS SQL数据库。刚刚安装了Centos Linux。 Apache / ftp可以工作。

但是我知道我想通过Codeigniter连接到Linux机器上的内部SQL服务器。 我已经安装了Yum mssql-php但是当我尝试在Codeigniter中连接时,它说“无法使用提供的设置连接到您的数据库服务器。”。

我的问题是我需要在Centos机器上将Codeigniter PHP与内部SQL 2008数据库连接起来吗?

我的Codeigniter设置为:

$db['default']['hostname'] = '***\***,1433';
$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;

在XAMPP Windows机器上,此设置使用sqlsrv运行良好。但那只是 在Windows机器上支持..

- 编辑 -

我的PHPinfo屏幕显示了这个: enter image description here

3 个答案:

答案 0 :(得分:0)

您需要在Linux机器上安装 php-mssql 扩展名。

第1步:安装所需的存储库

Remi对CentOS 6和Red Hat(RHEL)6的依赖

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Remi依赖CentOS 5和Red Hat(RHEL)5

rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

第2步:安装扩展程序

yum --enablerepo=remi php-mssql

第3步:启用扩展程序

编辑php.ini并加载php_mssql库并重新启动Apache Web服务器。

答案 1 :(得分:0)

目前我在Linux机器上安装mssql。 Codeigniter具有以下配置:

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

$db['sql']['dbprefix'] = '';

$db['sql']['pconnect'] = TRUE;

$db['sql']['db_debug'] = FALSE;

$db['sql']['cache_on'] = FALSE;

$db['sql']['cachedir'] = '';

$db['sql']['char_set'] = 'utf8';

$db['sql']['dbcollat'] = 'utf8_general_ci';

$db['sql']['swap_pre'] = '';

$db['sql']['autoinit'] = TRUE;

$db['sql']['stricton'] = FALSE;

PHP配置: enter image description here

Linux服务器上的FreetDs配置

在FreetDS配置中配置Linux机器上的连接非常重要。之后,您将在phpinfo中看到有一个Active连接,Codeigniter可以与SQL数据库连接。

enter image description here

答案 2 :(得分:0)

  

Codeigniter_project \ application \ config \ database.php

 $active_group = 'default';
    $query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'admin',
    'password' => 'Admin@321',
    'database' => 'xxxx',
    'dbdriver' => 'mysqli',
    //'dbdriver' => 'mysql',
    '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
);


$db['posdb']['hostname'] = 'xxx.xxx.xxx.xxx:1433';
//$db['posdb']['port'] = 1433; // or the port you configured on step 6
$db['posdb']['username'] = 'sa';
$db['posdb']['password'] = 'myself@123';
$db['posdb']['database'] = '
  

块引用

新&#39 ;;

$db['posdb']['dbdriver'] = 'mssql';
$db['posdb']['dbprefix'] = '';
$db['posdb']['pconnect'] = TRUE;
$db['posdb']['db_debug'] = FALSE;
$db['posdb']['cache_on'] = FALSE;
$db['posdb']['cachedir'] = '';
$db['posdb']['char_set'] = 'utf8';
$db['posdb']['dbcollat'] = 'utf8_general_ci';
$db['posdb']['swap_pre'] = '';
$db['posdb']['autoinit'] = TRUE;
$db['posdb']['stricton'] = FALSE;

考虑将posdb作为第二个在2012年与远程mssql连接的数据库

要在Controller中获取此功能

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contrller_name extends CI_Controller{     

    public function __construct()
    {
      parent::__construct();     
      $this->load->model('my_model');
      $this->load->library('session');    
    }
    public function connet(){       
        $connnet =  $this->my_model->connet();
        echo "<pre>";
        print_r($connnet);
        //var_dump($connnet);
    }
}   
?>
  

现在是模特

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_model extends CI_Model
{
     function __construct()
     {         
          parent::__construct();
          $this->load->helper('date');
          $this->pos_db= $this->load->database('posdb', TRUE);  // loading secondary remote database 

     }
     function connet(){        
        $sql ="SELECT TOP 10 * FROM dbo.Party";
    $query = $this->pos_db->query($sql);
    $result = $query->result_array();
    return $result;
     }

}?>
  

这对我来说很有用