1

I'm trying to do a select in one of my models of my codeigniter project. I have used the method you will see next on some other functions and never had problems since i was comparing ints and not strings. However, now i'm comparing strings the select is not done correctly since it doesn't return any results.

public function get_frasesr_by_cee($ficha_id){

    $query = $this->db->query("SELECT * FROM tabela_cee_r where id_ficha='".$ficha_id."';");

    if ($query->num_rows() > 0){
    //var_dump($query->result());

    $nomes_frasesr_cee;
    $i=0;

    foreach($query->result() as $row){
        $nomes_frasesr_cee[$i]=$row->cod_frase_r;
        //var_dump("Ola");
        $i++;
    }

    $max = sizeof($nomes_frasesr_cee);
    //var_dump($max);
    $dados_frasesr_cee;

    for($j = 0; $j < $max;$j++)
    {
        var_dump($nomes_frasesr_cee[$j]);
        $query1 = $this->db->query("SELECT * FROM frases_r where cod_frase_r='".$nomes_frasesr_cee[$j]."';");
        //var_dump($query1->result());
        foreach($query1->result() as $value){
                $dados_frasesr_cee[$j][0]=$value->id_frase_r;
                $dados_frasesr_cee[$j][1]=$value->cod_frase_r;
                $dados_frasesr_cee[$j][2]=$value->desc_frase_r;
                $dados_frasesr_cee[$j][3]=$value->desc_frase_r_ing;
                $dados_frasesr_cee[$j][4]=$value->desc_frase_r_esp;
                $dados_frasesr_cee[$j][5]=$value->diretiva_frase_r;
        }
    }
    var_dump($dados_frasesr_cee);
    return $dados_frasesr_cee;

} else {
        return;
    }

}

I guess the select considers this var $nomes_frasesr_cee[$j] empty and she is not. If I replace the var directly by some string I have on database it works perfectly, so I don't get what is the problem with the var.

Any hints? Thank you!

Result of the var_dump($query1);

object(CI_DB_mysql_result)#21 (8) { ["conn_id"]=> resource(29) of type (mysql link persistent) ["result_id"]=> resource(43) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(0) ["row_data"]=> NULL }
4
  • Is it an array? if so, you have to add comma's between them. Commented Jun 4, 2013 at 9:38
  • did u print the query ...like what kind of query is being fiered??\ Commented Jun 4, 2013 at 9:43
  • @kees sonnema, sorry, but commas between what? thank you! Commented Jun 4, 2013 at 10:47
  • Nevermind, I thought it could be an array. But it's not the problem. forgot what I've said :) Commented Jun 4, 2013 at 10:48

2 Answers 2

1

You do not escape the string ($nomes_frasesr_cee[$j]) and that probably leads to an invalid statement. In order to create a statement that is valid try this:

$query1 = $this->db->query("SELECT * FROM frases_r where cod_frase_r=?;", array($nomes_frasesr_cee[$j]));
Sign up to request clarification or add additional context in comments.

7 Comments

i tried sjkm, dont work :( i'm really missing something here on this query in the part i add the array var.
print the sql statement in your script and post it here so we can see what is wrong with it
We want to know how the query looks like (string). Please post that.
Sorry, but im not getting what you pretend. You mean, like if it had any results?
print "SELECT * FROM frases_r where cod_frase_r='".$nomes_frasesr_cee[$j]."';"
|
1

i have no idea why are you creating an extra array and looping through it again to get the same value.. and i see you are using the created array nowhere in the code(atleast in posted code)

try this

public function get_frasesr_by_cee($ficha_id){

$query = $this->db->query("SELECT * FROM tabela_cee_r where id_ficha='".$ficha_id."'");

if ($query->num_rows() > 0){

foreach($query->result() as $row){
    $query1 = $this->db->query("SELECT * FROM frases_r where cod_frase_r='".$row->cod_frase_r."'");
    //var_dump($query1->result());
    foreach($query1->result() as $value){
            $dados_frasesr_cee[]=$value->id_frase_r;
            $dados_frasesr_cee[]=$value->cod_frase_r;
            $dados_frasesr_cee[]=$value->desc_frase_r;
            $dados_frasesr_cee[]=$value->desc_frase_r_ing;
            $dados_frasesr_cee[]=$value->desc_frase_r_esp;
            $dados_frasesr_cee[]=$value->diretiva_frase_r;
    }        

var_dump($dados_frasesr_cee);
return $dados_frasesr_cee;

} else {
    return;
}

} 

NOTE: you really have to go through the CI's userguide and use active_record...JOINS mostly with join you can do this with one query..

example

public function get_frasesr_by_cee($ficha_id){   
 $this->db->select('t.*,f.*');
 $this->db->from('tabela_cee_r'.' t');
 $this->db->join('frases_r'.' f','t.cod_frase_r=f.cod_frase_r','left');
 $this->db->where('t.id_ficha',$ficha_id) 
 return $this->db->get();
});

1 Comment

you are right, i dont need that extra cicle, i will remove it, thank you for that. But that doesn't solve my problem. Either the way you exposed to me does not work, i'm really missing something in that part of the select, he just doesnt get the value of my var, i dont know why since if i print it before the select the value is there. could i be missing a ", ' or some function so that he can understand the value of that var? Even the row->cod_frase_r gives no results :O

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.