Welcome Guest, Not a member yet? Register   Sign In
Fatal Error after creating second function
#1

[eluser]NateL[/eluser]
Very odd, I created my first function and this function works very well:



model: animal_model.php
Code:
function getAllAnimals()
    {
        $query = $this->db->get('available');
        
        if ($query->num_rows() > 0 )
        {
            return $query->result();
        }else{
            show_error('Nothing in the database!');
        }
    }

However, inside my 'available' table, I have a visibility feature. 'visible' is on or off with a 1 or a 0

Code:
function getAvailableAnimals()
    {
        $query = $this->db->where('visible', 1);
        //print_r($query);
        
        if ($query->num_rows() > 0 )
        {
            return $query->result();
        }else{
            show_error('Nothing is currently available.');
        }
    }
in essence, returns "GET * WHERE visible = 1"

the function getAvailableAnimals() is essentially a copy/paste of getAllAnimals()

However, i'm this error:
Fatal error: Call to undefined method CI_DB_mysql_driver::num_rows() in system_files/application/models/animal_model.php on line 26

Now, I've noticed my error, but I'm not quite sure how to fix it.

What I need it to say is "GET * FROM available WHERE visible = 1"

---
As I was typing out this message, realized I was not selecting my table named 'available'...I found a solution that I tested to work..but i'm wondering if there may be a better way to write this line?

$query = $this->db->get('available', $this->db->where('visible', 1));

Thanks Smile
#2

[eluser]xzela[/eluser]
Hi,

You can use the Active Record feature by doing this:
Code:
$this->db->from('available');
$this->db->where('visible', 1);
$query = $this->db->get();

if($query->num_rows() > 0) {
return $query->results();
}
else {
return null; //found nothing;
}

It may look like more code, but it's much easier to read. Plus if you ever wanted to expand it at a later date, doing so will not result in an entire rewrite of the query. You can easily plug in extra joins and where clause
#3

[eluser]NateL[/eluser]
Awesome! I appreciate it very much Big Grin
#4

[eluser]NateL[/eluser]
O I'm getting another error with that code:

Fatal error: Call to undefined method CI_DB_mysql_result::results()

edit: nevermind Smile

should be result();
not results();

I seem to always figure it out after posting Big Grin
#5

[eluser]xzela[/eluser]
ah, yep, my mistake. sorry about that

Thanks point that out.

Good luck on your site.




Theme © iAndrew 2016 - Forum software by © MyBB