0

For some weird reason when i attempt to load the database no results are being returned.

class Customer_model extends CI_Model {

    public function fetch_email_list() {
        $DB1 = $this->load->database('orders', TRUE);

        if ( $this->load->database('orders') === FALSE ){
            echo 'no database';
        }

        $results = $DB1->query("SELECT * FROM email_list");

       return $results->result_array();

   }
}

I've checked my database config

$db['orders']['hostname'] = 'localhost';
$db['orders']['username'] = 'db_user';
$db['orders']['password'] = 'password';
$db['orders']['database'] = 'db_name';
$db['orders']['dbdriver'] = 'mysql';
$db['orders']['dbprefix'] = '';
$db['orders']['pconnect'] = FALSE;
$db['orders']['db_debug'] = TRUE;
$db['orders']['cache_on'] = FALSE;
$db['orders']['cachedir'] = '';
$db['orders']['char_set'] = 'utf8';
$db['orders']['dbcollat'] = 'utf8_general_ci';
$db['orders']['swap_pre'] = '';
$db['orders']['autoinit'] = TRUE;
$db['orders']['stricton'] = FALSE;

How do i solve?

user892134
  • 3,078
  • 16
  • 62
  • 128
  • 1
    You don't need that if statement, You've already loaded the database in ``$DB1`` variable, and without the second parameter set to TRUE it won't return a database object (You're essentially loading the same db twice the first one you're setting it into a custom variable & the second time you're "in your if" your assigning it as the default db e.g: ``$this->db``) – ahmad Mar 08 '16 at 14:15
  • ok i've removed the if statement. I have two functions in this model class, one can access the database and one can't. Any idea why? – user892134 Mar 08 '16 at 14:18
  • You're loading the database into a local variable, If you want to load it for all your methods inside this model then define a protected variable & use it across methods; I have added an example in my answer ... check it out. – ahmad Mar 08 '16 at 15:07

3 Answers3

1

No database is added in database.php. And this use $DB1 = $this->load->database('orders', TRUE); for if we have multiple Databases only.

Set default DB

$db['orders']['database'] = 'orders';

In Code

public function fetch_email_list() {
    $this->load->database();

    $query = $this->db->query("SELECT * FROM email_list");
    $result = $query->result_array();
    return $result
}

Codeigniter - multiple database connections

Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

You may define the database instance globally in a model so you can access it across methods as follows:

class Some_model extends CI_Model {

    // Our 2nd database
    protected $DB2;

    public function __construct () {
        parent::__construct();
        $this->DB2 =  $this->load->database('orders', TRUE);
    }

    public function some_method () {
        $q = $this->DB2->query('...');
    }

    public function some_other_method () {
        $q = $this->DB2->query('...');
    }   
}
ahmad
  • 2,709
  • 1
  • 23
  • 27
  • what fails? and what error message? could you elaborate? I've added this answer based on the OP comments he doesn't have any errors in configurations he just wants to access the db object in multiple methods within the model. – ahmad Mar 08 '16 at 19:04
  • The way you connecting DB is wrong. Cz when we use multiple DB, we have to configure this in database.php. Other than that we can't connect like this – Abdulla Nilam Mar 08 '16 at 19:07
  • Again, if you look at the OP question he doesn't have any issue with database configurations, since his configurations are there he only need to connect & expose the connection to all methods in the model – ahmad Mar 08 '16 at 19:10
0

Try this

$query = $this->db->get('email_list');
return $query->result_array();