Welcome Guest, Not a member yet? Register   Sign In
Select query not returning results (database problem?)
#1

[eluser]azavala[/eluser]
I must be tired and have overlooked something in my code. I hope thats the case anyway. Basically, I have a simple model that selects a database entry in a table by a specified number (ID#). The controller takes the user specified ID# and calls the model to pull up that record. The problem is that even though the table I'm trying to pull records from has plenty of entries in it, the model always returns FALSE (see code below) as if no records were found. I tried changing the query to select data differently, I tried changing the AR code in the model (ie using $this->db->where() with this->db->get(), etc). Always nothing.

I tried running the same query in my mysql database admin app and it also returns nothing. I can, however, select * from the table and get the entries. Also, the other models that work with this table function just fine. The other models add and delete records from the same table without any issues. I just can't seem to select a specific entry, regardless if it be by it's ID# or name, or whatever.

Been working on this project for a long time and I have a few other models that work the same way with other tables. Never had a problem like this. Is there something wrong with my db table? Should I try creating a new one to replace it and give it a try?

Here's my code incase...

Model:
Code:
function getCategory($cat_id)
    {
        $this->db->where('video_cat_id', $cat_id);
        $catquery = $this->db->get('videos_categories');
        
        if (!$catquery->num_rows() > 0)
        {
            // Category was not found in the database
            return FALSE;
        }
        else
        {    
            // Category found, return data
            $row = $catquery->row_array();
            
            $vidCategory['title'] = $row['video_cat_name'];
            $vidCategory['id'] = $row['video_cat_id'];
            $vidCategory['description'] = $row['video_cat_description'];
            $vidCategory['tags'] = $row['video_cat_tags'];
            $vidCategory['url'] = $row['video_cat_url'];
            
            return $featureVid;
        }
    }

Controller:
Code:
$catData = $this->videos_model->getCategory($data);
                
                // Check for a valid category
                if (!$catData)
                {
                    // Invalid Category ID
                    show_error('Invalid Category ID');
                }
                else
                {
                    // Category ID Valid, display edit form
                    $this->page_data['category_data'] = $catData;

                    // Output
                                        $this->page_data['content_view'] = $this->load->view('admin/admin_response', $this->page_data, TRUE);        
                    $this->load->view('admin_page', $this->page_data);    
                }

Appreciate any input on this...
#2

[eluser]mddd[/eluser]
I think the problem is in this statement
Code:
if (!$catquery->num_rows() > 0)

You are comparing "not num_rows()" with 0. That's not going to give you what you want.
I don't even know what 'not (some number) > 0' would give you. I guess 0 will also be turned into a boolean? Anyway, it is not the right way.
Rewrite your 'if'. Maybe make it 'if (num_rows()) { // yes it's okay } else { // not ok }'..
#3

[eluser]azavala[/eluser]
whups! thanks for posting
#4

[eluser]azavala[/eluser]
Erm, actually the problem was was in my model but was because of something else. The end of the success/true part of the logic was returning the wrong variable.

Code:
return $featureVid;

should have been

Code:
return $vidCategory;




Theme © iAndrew 2016 - Forum software by © MyBB