Welcome Guest, Not a member yet? Register   Sign In
Query multiple databases
#1

[eluser]Eildydar[/eluser]
I am trying to query multiple databases within the same page and I am having trouble getting it to work. in my model I have the following.

Code:
foreach($query->result() as $row) {
          $data[] = $row;
      }
And that works fine but if I try and add

Code:
$mysql = $this->load->database('default');
$odbc = $this->load->database('odbc');

$query = $mysql->query("SELECT * FROM db");
foreach($query->result() as $row) {
          $q = $odbc->query("SELECT * FROM otherDb WHERE field = $row->field");
          $result = $q->result()
          $return = array_merge($row, $result);
          $data[] = $return;
      }

But I keep getting an error stating "You have not selected a database type". This is a code igniter error and doesn't show up in any logs anywhere for php or apache.

Is this something that should be possible?
#2

[eluser]meigwilym[/eluser]
The guide says you need to pass TRUE as a 2nd argument to the load->database() method.

Mei
#3

[eluser]Eildydar[/eluser]
sorry yes I was passing true,
#4

[eluser]meigwilym[/eluser]
And did you set the $db['default']['dbdriver'] and $db['osbc']['dbdriver'] correctly?
#5

[eluser]Eildydar[/eluser]
yes they are set exactly the same one just points to a different server (this is not my end goal but set up that way for testing since that was best way to ensure no issues with config). Copy and paste just changing the host.
#6

[eluser]Eildydar[/eluser]
Any other ideas? Is this something that CodeIgniter will even support?
#7

[eluser]Eildydar[/eluser]
So I didn't think to do this before but I tried to just use the odbc link and it doesn't work at all. I have included the entire database.php file below. Not sure if I need to change anything in any other files.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| DATABASE CONNECTIVITY SETTINGS
| -------------------------------------------------------------------
| This file will contain the settings needed to access your database.
|
| For complete instructions please consult the 'Database Connection'
| page of the User Guide.
|
| -------------------------------------------------------------------
| EXPLANATION OF VARIABLES
| -------------------------------------------------------------------
|
| ['hostname'] The hostname of your database server.
| ['username'] The username used to connect to the database
| ['password'] The password used to connect to the database
| ['database'] The name of the database you want to connect to
| ['dbdriver'] The database type. ie: mysql.  Currently supported:
     mysql, mysqli, postgre, odbc, mssql, sqlite, oci8
| ['dbprefix'] You can add an optional prefix, which will be added
|     to the table name when using the  Active Record class
| ['pconnect'] TRUE/FALSE - Whether to use a persistent connection
| ['db_debug'] TRUE/FALSE - Whether database errors should be displayed.
| ['cache_on'] TRUE/FALSE - Enables/disables query caching
| ['cachedir'] The path to the folder where cache files should be stored
| ['char_set'] The character set used in communicating with the database
| ['dbcollat'] The character collation used in communicating with the database
|     NOTE: For MySQL and MySQLi databases, this setting is only used
|      as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7
|     (and in table creation queries made with DB Forge).
|      There is an incompatibility in PHP with mysql_real_escape_string() which
|      can make your site vulnerable to SQL injection if you are using a
|      multi-byte character set and are running versions lower than these.
|      Sites using Latin-1 or UTF-8 database character set and collation are unaffected.
| ['swap_pre'] A default table prefix that should be swapped with the dbprefix
| ['autoinit'] Whether or not to automatically initialize the database.
| ['stricton'] TRUE/FALSE - forces 'Strict Mode' connections
|       - good for ensuring strict SQL while developing
|
| The $active_group variable lets you choose which connection group to
| make active.  By default there is only one group (the 'default' group).
|
| The $active_record variables lets you determine whether or not to load
| the active record class
*/

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'binscan';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;

$db['odbc']['hostname'] = 'server';
$db['odbc']['username'] = 'root';
$db['odbc']['password'] = 'password';
$db['odbc']['database'] = 'binscan';
$db['obdc']['dbdriver'] = 'mysql';
$db['odbc']['dbprefix'] = '';
$db['odbc']['pconnect'] = FALSE;
$db['odbc']['db_debug'] = TRUE;
$db['odbc']['cache_on'] = FALSE;
$db['odbc']['cachedir'] = '';
$db['odbc']['char_set'] = 'utf8';
$db['odbc']['dbcollat'] = 'utf8_general_ci';
$db['odbc']['swap_pre'] = '';
$db['odbc']['autoinit'] = TRUE;
$db['odbc']['stricton'] = FALSE;

/* End of file database.php */
/* Location: ./application/config/database.php */
#8

[eluser]Eildydar[/eluser]
Any ideas?
#9

[eluser]CroNiX[/eluser]
Connecting to Multiple Databases
Code:
$mysql = $this->load->database('default', TRUE);
$odbc = $this->load->database('odbc', TRUE);
#10

[eluser]CroNiX[/eluser]
Oh, you said you were. Sorry.




Theme © iAndrew 2016 - Forum software by © MyBB