Database Returning no results

997 Views Asked by At

I've recently upgraded from perfectly working CodeIgniter v2.2.0 to CodeIgniter v3.0.1 for better session handling. In doing so all calls to the database regardless of which $database['dbdriver'] is used return nothing. I've also tried inputting a $config array directly into the database loader like so: $this->load->database($config); but this didn't work either. I've tried using the PDO and MySQLi drivers and both return the same thing:

MySQLi:

string 'SELECT *
FROM `ns_users`' (length=24)
object(CI_DB_mysqli_result)[26]
  public 'conn_id' => 
    object(mysqli)[24]
      public 'affected_rows' => null
      public 'client_info' => null
      public 'client_version' => null
      public 'connect_errno' => null
      public 'connect_error' => null
      public 'errno' => null
      public 'error' => null
      public 'error_list' => null
      public 'field_count' => null
      public 'host_info' => null
      public 'info' => null
      public 'insert_id' => null
      public 'server_info' => null
      public 'server_version' => null
      public 'stat' => null
      public 'sqlstate' => null
      public 'protocol_version' => null
      public 'thread_id' => null
      public 'warning_count' => null
  public 'result_id' => 
    object(mysqli_result)[25]
      public 'current_field' => null
      public 'field_count' => null
      public 'lengths' => null
      public 'num_rows' => null
      public 'type' => null
  public 'result_array' => 
    array (size=0)
      empty
  public 'result_object' => 
    array (size=0)
      empty
  public 'custom_result_object' => 
    array (size=0)
      empty
  public 'current_row' => int 0
  public 'num_rows' => null
  public 'row_data' => null

PDO:

string 'SELECT *
FROM `ns_users`' (length=24)
object(CI_DB_pdo_result)[26]
  public 'conn_id' => 
    object(PDO)[23]
  public 'result_id' => 
    object(PDOStatement)[25]
      public 'queryString' => string 'SELECT *
FROM `ns_users`' (length=24)
  public 'result_array' => 
    array (size=0)
      empty
  public 'result_object' => 
    array (size=0)
      empty
  public 'custom_result_object' => 
    array (size=0)
      empty
  public 'current_row' => int 0
  public 'num_rows' => null
  public 'row_data' => null

The simple query I'm trying to run is, so that I can see that data is being pulled from the database:

SELECT * FROM `ns_users`

The table is definitely not empty, it contains more than 3 million login records, i've even tried to throw in a limit of 1 in case the amount of records/data was too much.

Am I missing anything that is supposed to be in my configuration or something? the following is my configuration (keep in mind I have CI set up to handle multiple applications on the same CI installation)

config/database.php

<?php
$db['users'] = array(
    'dsn'      => 'mysql:host=localhost;dbname=apps_control',
    'hostname' => 'localhost',
    'username' => '***********',
    'password' => '***********',
    'database' => 'apps_control',
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => TRUE,
    'failover' => array(),
    'save_queries' => TRUE
);
define('USERS_DB_GROUP', 'users');

controllers/test.php

<?php
class Test extends CI_Controller {
    public function __construct() {
        parent::__construct();
    }
    public function index(){
        $this->load->model('Users');
        var_dump($this->Users->get_all_users());
        exit;
    }
}

models/users.php

<?php
class Users extends CI_Model {

    private $table_name;
    private $udb;

    public function __construct() {
        parent::__construct();
    }
    public function init($config = null){
        if(!empty($config) && is_array($config))
            $this->udb = $this->load->database($config, TRUE);
        else
            $this->udb = $this->load->database(USERS_DB_GROUP, TRUE);
        if(!$this->table_name)
            $this->table_name = $this->udb->dbprefix('ns_users');
    }
    public function get_all_users(){
        $this->init();
        $result = $this->udb->get($this->table_name);
        var_dump($this->udb->last_query());
        var_dump($result);exit;
        if($result && $result->num_rows > 0){
            return $result->result_array();
        }
        return false;
    }
}

Any help is appreciated, I'm just at a loss of what I'm missing here.

1

There are 1 best solutions below

0
On BEST ANSWER

Not sure why var_dump($result) shows your num_rows 0

Try to compare your result using num_rows() (as function)

if($result && $result->num_rows() > 0){

But I would write the function like this

public function get_all_users()
{
    $this->init();
    $result = $this->udb->get($this->table_name)->result_array();

    if($result)//check if our array empty or not.
    {
        return $result;
    }
    return false;
}