user892134
user892134

Reputation: 3214

Codeigniter $this->load->database not returning results

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?

Upvotes: 0

Views: 2973

Answers (3)

Visarut Sae-Pueng
Visarut Sae-Pueng

Reputation: 353

Try this

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

Upvotes: 0

ahmad
ahmad

Reputation: 2729

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('...');
    }   
}

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

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

Upvotes: 1

Related Questions