开发者

Query in code igniter keeps returning a "1"

开发者 https://www.devze.com 2022-12-12 06:40 出处:网络
I am a code igniter noob.I have a controller like the following: <?php class Welcome extends Controller {

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] =&g开发者_运维技巧t; 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...


$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?


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_guide/database/results.html. There's no shortcut.


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.


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');


I did have a similar problem. My code is outputing a large PDF and total execution time was above 60 seconds. I thing the connection to the database as lost it self.

Try $this->db->reconnect(); and don't forget to have $db['default']['db_debug'] = TRUE; in your database.php.

0

精彩评论

暂无评论...
验证码 换一张
取 消