Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Fatal error: Call to a member function num_rows() on a non-object
#1

[eluser]patak[/eluser]
Hi,

I am a beginner in CI and objected oriented php at all so my question might be stupid. But I'm stuck.
I play with CI and try to develop my first application which handles data in database.

Follows a controller

Code:
<?php

class Kontakty extends Controller {

    function kontakty()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['query'] = $this->db->get('cmf_kontakty_osoby');
        $this->load->view('kontakty/v_kontakty', $data);
        
    }
}

and a view file

Code:
<?php
$this->load->view('frontend/header');
?>

<!-- ------------------------------------------------------------------------------------------------ -->
<!-- ZACATEK content.php -->
<!-- ------------------------------------------------------------------------------------------------ -->

            <div id="post">
                <div class="entry">
                    <h1 class="title">Kontakty</h1>
                    &lt;?php
                        if ($query->num_rows() > 0) //this is line 13
                        {
                            $this->table->set_heading('ID osoby', 'Jméno', 'Příjmení', 'Email', 'Firma ID');
                            echo $this->table->generate($query);
                            $query->free_result();
                            $this->table->clear();
                        }
                    ?&gt;
                </div>&lt;!-- end #entry --&gt;    
            </div>&lt;!-- end #post --&gt;
        </div>&lt;!-- end #wraper-content --&gt;
    </div>&lt;!-- end #content --&gt;
&lt;?php
$this->load->view('frontend/sidebar');
?&gt;
        
&lt;!-- ------------------------------------------------------------------------------------------------ --&gt;
&lt;!-- KONEC content.php --&gt;
&lt;!-- ------------------------------------------------------------------------------------------------ --&gt;

&lt;?php
$this->load->view('frontend/footer');
?&gt;

pieces from autoload file
Code:
$autoload['libraries'] = array('database', 'table');
Code:
$autoload['helper'] = array('form', 'url');

When point firefox to http://127.0.0.1/cmf/index.php/kontakty/kontakty I get an errors
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: kontakty/v_kontakty.php

Line Number: 13
and
Code:
Fatal error: Call to a member function num_rows() on a non-object in C:\Users\user\Documents\xampp\htdocs\cmf\system\application\views\kontakty\v_kontakty.php on line 13

As you can see I am using xampp (PHP Version 5.2.8, and MySQL 5.1.30-community) on vista

Could anyone tell me what is wrong?
#2

[eluser]Dewos[/eluser]
edit:

what's code $this->db->get('cmf_kontakty_osoby'); ?
#3

[eluser]Flemming[/eluser]
are you sure your query is generating any result at all? you could enable the application profiler :

http://ellislab.com/codeigniter/user-gui...iling.html

then comment out the line in your controller that loads the view. Refresh and you should see profiling info at the bottom of your page, which will show you the db query that is being executed.

This may help you figure out what is going wrong?
#4

[eluser]Zack Kitzmiller[/eluser]
First of all, it would be best to move all of the DB code into a Model, where it is supposed to be. Also, do a print_r() in the controller of $data['query'] to see if you're getting any result.
#5

[eluser]patak[/eluser]
[quote author="Dewos" date="1257884791"]edit:

what's code $this->db->get('cmf_kontakty_osoby'); ?[/quote]
This might produce SELECT * FROM cmf_kontakty_osoby
#6

[eluser]patak[/eluser]
[quote author="flemming" date="1257885020"]are you sure your query is generating any result at all? you could enable the application profiler :

http://ellislab.com/codeigniter/user-gui...iling.html

then comment out the line in your controller that loads the view. Refresh and you should see profiling info at the bottom of your page, which will show you the db query that is being executed.

This may help you figure out what is going wrong?[/quote]
Here is profiling info:
Code:
/kontakty/kontakty
    
kontakty
    
2,001,056 bytes
  SROVNÁVACÍ TESTY  
Loading Time Base Classes      0.0380
Controller Execution Time ( Kontakty )      0.0607
Total Execution Time      0.0991
    
  POSTOVANÁ DATA  
Žádná POSTovaná data
  :  cmf   DOTAZY: 1  
0.0006      SELECT *
FROM (`cmf_kontakty_osoby`)
But I ma not sure if I understand to it well :-(
#7

[eluser]patak[/eluser]
[quote author="techneke" date="1257885518"]First of all, it would be best to move all of the DB code into a Model, where it is supposed to be. Also, do a print_r() in the controller of $data['query'] to see if you're getting any result.[/quote]
DB code in model - yes I'll do it.
From print_r($data['query']); I got
Code:
CI_DB_mysql_result Object ( [conn_id] => Resource id #32 [result_id] => Resource id #38 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 2 [row_data] => )
So I mean that I got two rows of results - which is OK, in database are two entries
#8

[eluser]mattpointblank[/eluser]
You're not doing anything with the result resource:

Code:
$data['query'] = $this->db->get('cmf_kontakty_osoby');


You need to actually use the results functions:

Code:
$data['query'] = $this->db->get('cmf_kontakty_osoby')->result_array();
#9

[eluser]Zack Kitzmiller[/eluser]
I think this has to do with passing the object to the view. I'd change the code to handle more in the controller.

do it if's and $query->result(); in the controller.
#10

[eluser]patak[/eluser]
It works!

Thank you very much friends!




Theme © iAndrew 2016 - Forum software by © MyBB