CodeIgniter Forums
Returns result from DB even though it's empty! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Returns result from DB even though it's empty! (/thread-22159.html)



Returns result from DB even though it's empty! - El Forum - 08-31-2009

[eluser]felixk[/eluser]
Controller:
Code:
function index()
    {
        $data['title'] = "Fabriken Fredag | Bildgalleri";
        $this->load->library('pagination');

        $config['base_url'] = site_url('/galleri/index/');
        $config['total_rows'] = $this->db->count_all_results('albums');
        $config['per_page'] = 4;
        $config['uri_segment'] = 3;
        $config['first_link'] = 'Första';
        $config['last_link'] = 'Sista';
        $this->pagination->initialize($config);
                
        //DB
                //Doesn't matter which one on of $data['albums'] i'm chosing.
        $data['albums'] = $this->db->get('albums');
        //$data['albums'] = $this->galleryModel->All($config['per_page'], $this->uri->segment(3));
        $this->load->view('galleri_view', $data);    
}

Viewer:
Code:
<?php if(empty($albums)): echo "Empty! :S"; ?>
      
        <?php else: ?>
        <h2>Bildgalleri!</h2>
            &lt;?php foreach($albums->result() as $row): ?&gt;
                &lt;?php echo $row->title; ?&gt;<br />
            &lt;?php endforeach; ?&gt;
        &lt;?php endif; ?&gt;



Returns result from DB even though it's empty! - El Forum - 08-31-2009

[eluser]felixk[/eluser]
If I do a var_dump($albums) the result is:
Quote:object(CI_DB_mysql_result)#18 (7) { ["conn_id"]=> resource(30) of type (mysql link persistent) ["result_id"]=> resource(40) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(0) ["row_data"]=> NULL }



Returns result from DB even though it's empty! - El Forum - 08-31-2009

[eluser]bretticus[/eluser]
Don't check empty. In the manual the examples show checking num_rows(). So try checking $albums->num_rows().


Returns result from DB even though it's empty! - El Forum - 08-31-2009

[eluser]felixk[/eluser]
Thanks! It's weird that the exact same code, but in other controllers and views files worked as I wanted.