CodeIgniter Forums

Full Version: Call to a member function num_rows() on a non-object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]trumnation[/eluser]
Hi, I'm having troubles with the num_rows() function. I keep getting the following error:

Fatal error: Call to a member function num_rows() on a non-object in E:\Server 2\Root\application\models\userdatamodel.php on line 1296

here is the code I am using:

Code:
function compare($friendId, $userId = '')
        {
                $userId = (trim($userId) == '') ? $this->session->userdata('user_id') : $userId;

                $this->db->where('approved_status', 'yes');
                $this->db->where('((user_id=' . $userId . ' AND friend_id=' . $friendId . ')');
                $this->db->or_where('(friend_id=' . $userId . ' AND user_id=' . $friendId . '))');
                $this->db->limit(1, 0);
                $Query = $this->db->get('friends_list');
                echo $this->db->last_query();
                if ($Query->num_rows() > 0) return true;
                else  return false;
        }

Can anyone tell me why I'm getting this error?

Thanks!

Derek

El Forum

[eluser]mattpointblank[/eluser]
Does $this->$query->num_rows() work?

El Forum

[eluser]trumnation[/eluser]
No, that doesn't work. It gives me an empty property error. Is there anything else that could be causing this problem?

El Forum

[eluser]alboyd[/eluser]
I'd start by doing a echo var_dump($Query) and see what it looks like. It's presumably empty - but that's odd.

Try writing the query out without using activerecord and then executing it. Basically isolating what could be the cause.

El Forum

[eluser]trumnation[/eluser]
You would be correct about that - it is empty. I did try writing the query out and not using activerecord - I even just tried to select the entire table and it still didn't work... here is the code I used:

Code:
$sql1 = "SELECT * FROM `friends_list`";
                $Query = $this->db->query($sql1);
                if ($Query->num_rows() > 0) return true;
                else  return false;

This is such a simple task.. just grab data from a table and return true if there was actually anything in the table..... but it doesn't work. Is there something in the codeigniter core that causes this, or am I just missing something really simple?

El Forum

[eluser]InsiteFX[/eluser]
Code:
var $CI;    // The CI object

$this->CI =& get_instance();

$sql1 = "SELECT * FROM `friends_list`";
                $Query = $this->CI->db->query($sql1);
                if ($Query->num_rows() > 0) return true;
                else  return false;

I had the same problem designing the new menu system for the Fresh CMS.
I had autoloaded the databse and still gave me errors on num_rows.

You can also do it like this:

Code:
$query = $this->CI->db->query("SELECT * FROM `friends_list`");

Enjoy
InsiteFX

El Forum

[eluser]alboyd[/eluser]
Code:
$sql1 = "SELECT * FROM `friends_list`";
                $Query = $this->db->query($sql1);
                if ($Query->num_rows() > 0) return true;
                else  return false;
What's with 'friends_list' in the query? That won't work I don't think.

Try just "SELECT * FROM friends_list"

As long as you are doing this in a model I don't think why you would need to get a copy of CI ?

EDIT: btw - do you have error logging on? Because I think you are not seeing an error - how else could it not return either true or false to num_rows()? I'd say the query itself is failing...

El Forum

[eluser]trumnation[/eluser]
@insiteFX - I tried what you suggested, however on the var $CI; it gives me an unexpected T_VAR error.. I am using this function in a model. Does this make any difference rather than using a controller?

@alboyd - No matter whether I have the quotes around friends_list or not, it still doesn't work. Good idea on turning on error logging. It did produce an error, although I am at a complete loss on how to fix it, wouldn't this be a problem with the database driver?

Code:
Severity: Warning  --&gt; mysql_query() [<a href='function.mysql-query'>function.mysql-query</a>]: Unable to save result set E:\Server 2\Root\CodeIgniterCore\database\drivers\mysql\mysql_driver.php 125

El Forum

[eluser]Zorancho[/eluser]
Try $this->db->num_rows() instead of $query->num_rows()

El Forum

[eluser]trumnation[/eluser]
Still haven't been able to find what is causing this error and how to fix it. Is there anything else i'm missing?
Pages: 1 2