ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

CodeIgniter App getting data from the wrong mySQL table?

October 07, 2008 5:13am

Subscribe [3]
  • #1 / Oct 07, 2008 5:13am

    Dave Blencowe

    28 posts

    Hey,

    Recently I have been coding the Blog section to my personal site which is based on Codeigniter and have run across a problem.
    I have made a model for getting things like posts etc from the database and then have the controller use the model to get the appropriate results yet when I ask for data from one table for comments it returns another table altogether and I just can’t see why.

    The section of the model

    function get_blog($id, $limit = NULL)
        {
            
            $this->db->order_by("id", "desc");
            if($limit !== NULL)
            {
                $this->db->limit($limit);
            }
            
            if($id)
            {
                $this->db->where('id', $id);
            }
            $blog = $this->db->get('content_blog');
            return $blog->row_array();
        }
        
        function get_comments($id)
        {
            $this->db->order_by('id', 'desc');
            $this->db->where('post_id', $id);
            $comments = $this->db->get('content_test');
            return $comments->row_array();
        }

    The section of the controller

    function comments()
        {
            $this->load->view('template/default/header');
            
            $id = $this->uri->segment(3);    
            $get_blog = $this->mdl_blog->get_blog($id, '1');        
    
            $get_comments = $this->mdl_blog->get_comments($id, NULL);
    
            $this->load->view('blog/comments/main_view', $get_blog);
            $this->load->view('blog/comments/comments_view', $get_comments);
            $this->load->view('blog/comments/comments_add');
            $this->load->view('template/default/footer');
        }

    These two views seem to have the the $get_blogs variable passed to them each time…

    $this->load->view('blog/comments/main_view', $get_blog);
            $this->load->view('blog/comments/comments_view', $get_comments);


    I’ve attached the files in case they are any use and I thank you for any help you might be able to provide.

    Thanks,
    Dave

  • #2 / Oct 07, 2008 5:23am

    xwero

    4145 posts

    And you get result form which other table?

  • #3 / Oct 07, 2008 5:39am

    Dave Blencowe

    28 posts

    content_blog instead of content_test

  • #4 / Oct 07, 2008 5:54am

    xwero

    4145 posts

    And you are sure

    $get_comments = $this->mdl_blog->get_comments($id, NULL);

    Isn’t

    $get_comments = $this->mdl_blog->get_blog($id, NULL);

    Because there is no second parameter in your get_comments method.

  • #5 / Oct 07, 2008 6:02am

    Dave Blencowe

    28 posts

    Yeah I am sure, Have removed that extra NULL now as it was needed earlier before I edited the code a bit. Still having the same problem :(.
    If you want me to do something like post the whole project somewhere I could?

  • #6 / Oct 07, 2008 6:21am

    xwero

    4145 posts

    You can try to debug it with $this->db->last_query() to see if the content_test sql statement actually runs

  • #7 / Oct 07, 2008 6:34am

    Dave Blencowe

    28 posts

    I’m just watching Stargate at the moment so I’ll give that a try in about 20 minutes :D

    Thanks a lot dude,
    Dave

  • #8 / Oct 07, 2008 7:03am

    Dave Blencowe

    28 posts

    Yeah, the last query run is:

    SELECT * FROM (`content_test`) WHERE `post_id` = '1' ORDER BY `id` desc
  • #9 / Oct 07, 2008 7:06am

    xwero

    4145 posts

    So get_comments should contain that data? You can check by using print_r.

  • #10 / Oct 07, 2008 7:11am

    Dave Blencowe

    28 posts

    print_r($get_comments); gives me Array ( )

  • #11 / Oct 07, 2008 7:13am

    Adam Griffiths

    316 posts

    You can also use

    [php]
    $this->db->from(‘content_blog’);
    [/php]

    It might make no difference, but it’s something to try.

    Oh and I’m still thrown on this line.

    [php]
    if($id)
    {
    ...
    }
    [/php]

    It’s really not needed.

  • #12 / Oct 07, 2008 7:18am

    Dave Blencowe

    28 posts

    It was in an earlier version, removed it now.

  • #13 / Oct 07, 2008 7:21am

    Dave Blencowe

    28 posts

    Changing the get_comments function in the model to this:

    function get_comments($id)
        {
            $this->db->order_by('id', 'desc');
            $this->db->where('post_id', $id);
            $this->db->from('content_blog');
            $comments = $this->db->get('content_test');
            
            return $comments->row_array();
        }

    Gives me the following error:

    A Database Error Occurred
    
    Error Number: 1052
    
    Column 'id' in order clause is ambiguous
    
    SELECT * FROM (`content_blog`, `content_test`) WHERE `post_id` = '1' ORDER BY `id` desc

    EDIT: Ignore that, I’m an idiot lol. Changing it from content_blog to content_test does nothing.

  • #14 / Oct 07, 2008 7:23am

    xwero

    4145 posts

    Yes because now you have two tables.

    My best guess is that the blog post has no comments.

  • #15 / Oct 07, 2008 7:25am

    Dave Blencowe

    28 posts

    Yeah I saw the error after I posted >_>
    And yeah, the blog post doesn’t have any comments but why does that make it move to content_blog?
    I’m going to insert a piece of data and try it now.

    EDIT: Ok, well it seems to be taking the variables/results from both of them…