user1511579
user1511579

Reputation: 1537

codeigniter database select comparing strings

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 }

Upvotes: 1

Views: 2603

Answers (2)

bipen
bipen

Reputation: 36531

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();
});

Upvotes: 1

sjkm
sjkm

Reputation: 3937

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]));

Upvotes: 1

Related Questions