1

In my CodeIgniter application, I'm trying to retrieve data from my database table. Here is my code:

database.php:

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

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "*****";
$db['default']['password'] = "****";

$db['default']['database'] = "mydbname";
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$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;

In Model.php:

$sql = "SELECT * from users";

$query = $this->db->query($sql);
print_r($query);

but it return like

CI_DB_mysqli_result Object (
    [conn_id] => 
    [result_id] => 
    [result_array] => Array ( ) 
    [result_object] => Array ( ) 
    [custom_result_object] => Array ( ) 
    [current_row] => 0 
    [num_rows] => 
    [row_data] => 
)
array(0) { }

See, it is an empty result. How can I get a populated result?

7
  • $this->db->select('*'); $this->db->from('users'); $query = $this->db->get(); echo $this->db->last_query(); return $query->result_array(); use this and check if the query is executing right or wrong? Commented Mar 31, 2016 at 13:05
  • try : print_r($query->result_array()); to get your resultset instead of the whole CI query object Commented Mar 31, 2016 at 13:05
  • @Vicky still empty array Commented Mar 31, 2016 at 13:09
  • @JBA empty Array() Commented Mar 31, 2016 at 13:09
  • run query in phpmyadmin Commented Mar 31, 2016 at 13:09

1 Answer 1

2

Do this:

<?php
$sql = "SELECT * from users";
$query = $this->db->query($sql);
if($query->num_rows() > 0) {
    print_r($query->result_array());
} else {
    $d = array();
    print_r($d);
}
?>
Sign up to request clarification or add additional context in comments.

25 Comments

@Vini did you use if condintion?
@PathikVejani yes i used if. In localhost it is working well. In live server issue occur.
Check weather database library is loaded or not
@Vini set 'db_debug' => TRUE and check
@PathikVejani if i set db_debug' => TRUE then this should happen Unable to connect to your database server using the provided settings. Filename: C:\Inetpub\vhosts\jobblogger.no\domian\system\database\DB_driver.php
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.