• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
$query->num_rows() - Fatal error: Call to a member function on a non-object

#1
[eluser]jbawarren[/eluser]
This is probably a simple problem, but I keep getting the error:

Fatal error: Call to a member function on a non-object

when i try to use the function:

$query->num_rows()

I have declared the database class ($autoload['libraries'] = array('database')Wink in the autoload file in the config folder.

can anyone tell me what I am doing wrong? I am just trying to follow the best practices that i noticed in the video tutorials.

Here is my code:

Code:
if($query->num_rows() > 0):
foreach($query as $row):
    echo form_hidden('id', $this->uri->segment(3));
    echo 'Login'.form_input('login', $row->login).'<br>';
    echo 'Pass'.form_input('pass', $row->pass).'<br>';
    echo 'Sitename'.form_input('sitename', $row->sitename).'<br>';
    echo 'URL'.form_input('url', $row->url).'<br>';
    echo 'Notes'.form_input('notes', $row->notes).'<br>';
    echo form_submit('submit', 'Update Password');
endforeach;
endif;

Thanks in advance.

#2
[eluser]BD-CI-Programmer[/eluser]
See the code

Code:
$query = $this->db->query($sql);
if($query->num_rows()>0)
{
$result  = $query->result();
foreach($result as $row)
{

// Your Code

} // End foreach
} // End If

#3
[eluser]jbawarren[/eluser]
Thanks, that was a big help.

My additional question would have been: how would you break up the query and view elements to follow the MVC framework pattern? But I realized that I was sending $query->result() from my model method instead of just the raw $query. This made me unable to perform $query->num_rows in the view. Now that i an just sending the $query, I can perform the num_rows check in my view, where it should be? (I think) I'm new to this framework/MVC thing, so I'm trying to be as strict as possible. Does this make sense?

#4
[eluser]JoostV[/eluser]
Actually, I would do num rows check in your model and use $query to fill a data array. Then send this array to the view. Then you can do whatever you want with this array in the view.

In this way, you will have handled all record harvesting in the model, and all displaying in the view.

Model:
Code:
$query = $this->db->query($sql);

foreach ($query->result_array() as $row) {
   $data['record'][] = $row;
}

$this->load->view('your_view', $data);

View:
Code:
foreach ($record as $row) {
   echo '<h1>' . $row['title'] . '</h1>'
}

Use result() instead of result_array() for object-style record retrieval, e.g. $row->title

#5
[eluser]jbawarren[/eluser]
Huge help Joost!

Thanks a lot.

#6
[eluser]m4rw3r[/eluser]
why not add the db result directly to record? The result is the same (the foreach is a waste of cpu and memory, if you don't need to make any changes to the data).
Code:
$query = $this->db->query($sql);

$data['record'] = $query->result_array();

$this->load->view('your_view', $data);

BTW. It is the controller that calls the view, the model only fetches the data.

#7
[eluser]jbawarren[/eluser]
Great tip. That make a lot of sense. Thanks m4rw3r.

#8
[eluser]JoostV[/eluser]
@m4rw3r

You're right. Stupid of me.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.