Codeigniter - 连接MySQL和SQL Server的致命错误

时间:2015-11-07 06:55:52

标签: php sql-server codeigniter-3

我需要在同一个应用程序中连接到MySQL和SQL Server。我的php版本5.6.11和codeigniter 3.0.1。我在下面建立连接的步骤:

  1. 我下载此文件并粘贴到xampp \ php \ ext

    php_sqlsrv_56_ts
    php_pdo_sqlsrv_56_ts

  2. 像这样编辑php.ini文件:

    延长= php_sqlsrv_56_ts.dll
    延长= php_pdo_sqlsrv_56_ts.dll

  3. database.php中

    $active_group = 'default';
    $query_builder = TRUE;
    
    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'vtp',
        'dbdriver' => 'mysqli',
        '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['biodb'] = array(
        'hostname' => '192.168.20.231',
        'username' => 'abzalali',
        'password' => '',
        'database' => 'pdata',
        'dbdriver' => 'sqlsrv',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'autoinit' => TRUE,
        'stricton' => FALSE,
    );
    

    控制器:

    <?php
    
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    
    class biodb extends CI_Controller {
    
        public function __construct() {
    
            parent::__construct();
            $this->sqlsrvr = $this->load->database('biodb', true);
        }
    
        public function index() {
            $query = $biodb->get('arb_protest_pp');
            foreach ($query->result() as $row)
                echo $row->id;
        }
    
    }
    

    然后我在url中调用控制器'biodb',并收到错误:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in C:\xampp\htdocs\vtp\system\database\DB_driver.php on line 771
    

    之后: 我在此函数

    中的“DB_Driver.php”中添加了一个新行“$this->db_select();
    public function simple_query($sql)
    {
        if ( ! $this->conn_id)
        {
            $this->initialize();
        }
        $this->db_select();
        return $this->_execute($sql);
    }
    

    然后我又得到了另一个错误:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in C:\xampp\htdocs\vtp\system\database\DB_driver.php on line 642
    

    我真的没有发现我错过了什么。我需要使用codeigniter活动记录这个sqlserver db。

1 个答案:

答案 0 :(得分:1)

过去我使用带有CodeIgniter的直接sqlsrv驱动程序遇到了很多问题。

我会通过在databbase.php配置文件中执行以下操作将sqlsrv转换为pdo_sql_srv:

$db['biodb'] = array(
    'hostname' => '192.168.20.231',
    'username' => 'abzalali',
    'password' => '',
    'database' => 'pdata',
    'dbdriver' => 'pdo',
    'subdriver' => 'sqlsrv',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'autoinit' => TRUE,
    'stricton' => FALSE,
);

我也很惊讶你在控制器中的索引方法上没有得到未定义的var错误。尝试将其更改为:

public function index() {
    //you need to call the database property you 
    //created in your construct which appears to be sqlsrvr
    $query = $this->sqlsrvr->get('arb_protest_pp');
    foreach ($query->result() as $row) {
        echo $row->id;
    }
}