1

I need to connect to MySQL and SQL Server in same application. My php version 5.6.11 and codeigniter 3.0.1. The steps I followed to establish the connection below:

  1. I download this file and paste into xampp\php\ext

    php_sqlsrv_56_ts
    php_pdo_sqlsrv_56_ts

  2. Edit php.ini file like this:

    extension=php_sqlsrv_56_ts.dll
    extension=php_pdo_sqlsrv_56_ts.dll

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,
);

Controller:

<?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;
    }

}

Then I call the controller 'biodb' in url, and getting error:

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

After that: I added a new line "$this->db_select();" in "DB_Driver.php" in this function

public function simple_query($sql)
{
    if ( ! $this->conn_id)
    {
        $this->initialize();
    }
    $this->db_select();
    return $this->_execute($sql);
}

And then I also getting another error:

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

I really didn't find out what I missed actually. And I need to work with codeigniter active record this sqlserver db.

4
  • you need to increase allowed memory size. Please used this code memory_limit = 512M in php.ini file. Commented Nov 7, 2015 at 7:03
  • I increase my memory_limit=512m then also getting error at line 147. My Server Name - ABZAL-JO and user is IDB-BISEW\abzalali. instead of server name I used local ip of my pc and user I used only ablzalali in the db configuration. is there any mistakes that I did? Commented Nov 7, 2015 at 7:14
  • This Link may be help for you. Commented Nov 7, 2015 at 7:31
  • How many rows are in your arb_protest_pp table? Commented Nov 7, 2015 at 10:05

1 Answer 1

1

I've run into many problems in the past using the the straight up sqlsrv driver with CodeIgniter.

I would convert your sqlsrv to pdo_sql_srv by doing the following in your databbase.php configuration file:

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

I'm also surprised you aren't getting an undefined var error on your index method in your controller. Try changing it to this:

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes, it works now, thank you very much. could please specify this comments as //you need to call the database property you //created in your construct which appears to be sqlsrvr
In your construct you defined the connection as "$this->sqlsrvr" so when you call this connection in any other method you need to use the "sqlsrvr" property you created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.