views:

337

answers:

4

I am a code igniter noob. I have a controller like the following:

<?php
class Welcome extends Controller {
    function Welcome()
    {
     parent::Controller(); 
     $this->load->model('usermodel');
    }

    function index()
    {
     $res = $this->db->get('users');
     die( print_r($res) );
    }
}

I have a table 'users' in my database and I have triple checked my database credentials. However, the print method keeps just printing a "1". Anyone know why this simple query would not work?

Also worth noting that print_r prints out an object and i did set active_record = true in my database.php file. In case it helps, this is the output from

print_r($this->db);

prints

CI_DB_mysql_driver Object ( [dbdriver] => mysql [_escape_char] => ` [delete_hack] => 1 [_count_string] => SELECT COUNT(*) AS [_random_keyword] => RAND() [ar_select] => Array ( ) [ar_distinct] => [ar_from] => Array ( ) [ar_join] => Array ( ) [ar_where] => Array ( ) [ar_like] => Array ( ) [ar_groupby] => Array ( ) [ar_having] => Array ( ) [ar_limit] => [ar_offset] => [ar_order] => [ar_orderby] => Array ( ) [ar_set] => Array ( ) [ar_wherein] => Array ( ) [ar_aliased_tables] => Array ( ) [ar_store_array] => Array ( ) [ar_caching] => [ar_cache_exists] => Array ( ) [ar_cache_select] => Array ( ) [ar_cache_from] => Array ( ) [ar_cache_join] => Array ( ) [ar_cache_where] => Array ( ) [ar_cache_like] => Array ( ) [ar_cache_groupby] => Array ( ) [ar_cache_having] => Array ( ) [ar_cache_orderby] => Array ( ) [ar_cache_set] => Array ( ) [username] => root [password] => my_password [hostname] => localhost [database] => bhr_development [dbprefix] => [char_set] => utf8 [dbcollat] => utf8_general_ci [autoinit] => 1 [swap_pre] => [port] => [pconnect] => 1 [conn_id] => [result_id] => [db_debug] => [benchmark] => 0 [query_count] => 0 [bind_marker] => ? [save_queries] => 1 [queries] => Array ( [0] => SELECT * FROM (`users`) ) [query_times] => Array ( [0] => 0 ) [data_cache] => Array ( ) [trans_enabled] => 1 [trans_strict] => 1 [_trans_depth] => 0 [_trans_status] => [cache_on] => [cachedir] => [cache_autodel] => [CACHE] => [_protect_identifiers] => 1 [_reserved_identifiers] => Array ( [0] => * ) [stmt_id] => [curs_id] => [limit_used] => ) 1

UPDATE I tried something like:

$query = $this->db->get("users");
     foreach($query->result() as $row){
      echo "hello";
     }
     die();

with no luck. Still get:

Fatal error: Call to a member function result() on a non-object in ...welcome.php...
+1  A: 

$res is a result object, not a string resulting from a successful query. Have you tried calling $res->num_rows() or iterating over $res->result() in a foreach loop and printing out the values of the columns (ex. echo $res->col_name)?

Also (I guess I should have asked this first), are there even any rows in that table?

Marc W
i understand that $res is an object. print_r will print an object. when i try num_rows i get "Call to a member function num_rows() on a non-object"...it's literally giving me back a "1". odd thing is that if i do a print_r on this->db, i get what looks like a valid database object with the query in there... "[queries] => Array ( [0] => SELECT * FROM (`users`) "
Tony
also, yes there are rows in the table.
Tony
In your database config file (`database.php`), do you have `$active_record = true;`? If not, the active record libraries won't get loaded and that might be why you aren't getting a proper result object.
Marc W
yes, i do. prob should have mentioned that in the post since that is a possible cause of this.
Tony
also worth noting that im not sure if it is returning a string "1" or an integer "1". either way something is screwy
Tony
Wow...that's really weird. I've been using CI for some time now and I can't recall it ever doing something like this. Apologies for not being able to help more. If I find something, I'll be sure to let you know, though.
Marc W
thanks for your help thus far. i agree this is really weird. guess i'll have to trace through the CI code.
Tony
A: 

Your query is not returning 1, that's coming from the die statement. The print_r function does not return values unless you pass the 2nd parameter. What is happening is the print_r function is evaluating to true and being printed by the die function.

If you want to see the query results, you have to use the result or result_array functions as explained at http://codeigniter.com/user%5Fguide/database/results.html. There's no shortcut.

bradym
A: 

The correct usage is:

$res = $this->db->get('users');

foreach ($res->result() as $row)
{
    echo $row->title;
}

The actual result data rows are not loaded until you iterate over them.

Joel L
that does not work, i updated my question
Tony
also i would imagine printing $res before iterating should print out an object (not the data)
Tony
A: 

For the query to work correctly, I'd expect you would need to initialize the database class first:

$this->load->database();
$res = $this->db->get('users');
jpwilksch