Welcome Guest, Not a member yet? Register   Sign In
Problem with data returned from query
#1

[eluser]Byrro[/eluser]
Hello,

I have a model with the following function:

Code:
function consulta_id_table() {
        
            $DBempresa = $this->load->database("...", TRUE);
            $DBempresa->select('id_table');
            $query = $DBempresa->get_where("vsk_{$codigo_cliente}_empresas", array('id' => '1'), '1');
            
            return $query->result();
        
        }

Then, in the Controller:

Code:
function header() {
            
        $this->load->model('Empresa');
            
        $returned_id_table = $this->Empresa->consulta_id_table();
        
        $data['test'] = $returned_id_table;
            
        $this->load->view('empresa', $data);
        
    }

When I print_r the variable $test in the view file, I have the following result:

Quote:Array ( [0] => stdClass Object ( [id_table] => 001 ) )

Just fine. But then I have problem with this: in the controller, I have do manipulate the variable $returned_id_table to use this result as an argument to call another Model method, instead of sending it to the view. I´m doing the following in the same controller method:

Code:
function header() {
            
        $this->load->model('Empresa');
            
        $returned_id_table = $this->Empresa->consulta_id_table();
        $id_table = $returned_id_table[0]->id_table;
        
        $data['test'] = $this->Empresa->dados_para_menu_gerenciador($id_table);
            
        $this->load->view('empresa', $data);
        
    }

It returns this error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Line: "$id_table = $returned_id_table[0]->id_table;"

I´ve tried with '0':
Code:
$id_table = $returned_id_table['0']->id_table;
but:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: 0

What´s going on? Could someone please help me find where is the mistake?
#2

[eluser]WanWizard[/eluser]
If I copy and paste that code in my controller here (and replace your method with a query that returns the same result ), $id_table contains the integer 1, and I don't get the notice error.

Are you sure your query returns a correct result?
#3

[eluser]danmontgomery[/eluser]
Code:
print_r($returned_id_table);
shows what?
#4

[eluser]Byrro[/eluser]
[quote author="noctrum" date="1282789300"]
Code:
print_r($returned_id_table);
shows what?[/quote]

When the where clause is commented out, print_r($returned_id_table) shows:

Quote:Array ( [0] => stdClass Object ( [id_table] => 001 [hashed_pim] => 05e3c00bdb22b2b7b6ed1d1358456ac1bdf28420 ) )

When I turn on the Where clause, it shows an empty array...

The variable $hashed_pim is passed from the URI: website.com/controller/empresas/hash_string

Inside the first model´s method, it receives the $hashed_pim but doesn't work with the Where clause. I think it´s a problem with the syntax when passing the arguments to Where...
#5

[eluser]danmontgomery[/eluser]
Quote:Call to a member function result() on a non-object in ” return $query->result(); ” on the second method

Means there's an error in your query.

Code:
echo $DBempresa->last_query();
after the get_where(), see what query exactly as it's being run.
#6

[eluser]Byrro[/eluser]
[quote author="noctrum" date="1282790219"]
Quote:Call to a member function result() on a non-object in ” return $query->result(); ” on the second method

Means there's an error in your query.

Code:
echo $DBempresa->last_query();
after the get_where(), see what query exactly as it's being run.[/quote]


That´s it! The last_query():

SELECT `id_table` FROM (`vsk_010121_empresas`) WHERE `hashed_pim` = '$hashed_pim'

It´s not sending to the Where clause the string of the variable, but the $variable name...

How can I fix that?
#7

[eluser]WanWizard[/eluser]
You example given doesn't mention this query, do we need to guess to what you have exactly coded?

You are passing a variable name as a string.
#8

[eluser]danmontgomery[/eluser]
[quote author="WanWizard" date="1282822462"]You example given doesn't mention this query, do we need to guess to what you have exactly coded?[/quote]

He created an identical thread that did have the relevant information in it: http://ellislab.com/forums/viewthread/165560/




Theme © iAndrew 2016 - Forum software by © MyBB