Return table data parameters, even if $query result is 0

Asked

Viewed 281 times

1

Good evening: I am developing a project that consists of all parameters in a table of parameters... However, in my SQL it only works with records above 0: Follow the code

public function getAcompanhantes($tipo)
{
    $consulta = $this->db->query("SELECT *, p.parametro as cidade FROM cadastroperfil AS cp, parametro as p WHERE cp.verificado = '1' AND cp.codCategoria = {$tipo} AND cp.codCidade = p.idParametro")->result();

        foreach($consulta as &$valor){
            $sql =  "SELECT * FROM parametro WHERE idParametro = {$tipo}";
            $valor->Categoria = $this->db->query($sql)->row();
        }

    return $consulta;
}

Even if I change SQL to the following way, it doesn’t work:

SELECT * FROM cadastroperfil 
LEFT JOIN parametro ON idParametro = cadastroperfil.codCategoria
WHERE cadastroperfil.codCategoria = {$tipo}

Note that in the $query, I requested the parameters so that I can know the city where the model is, and also the profile data. And in the following foreach, I search the category data, so I know if it is a man, woman and etc. But, he only passes the parameters in the second, if the first has records greater than zero. I’d like to see if anyone has any suggestions. Thank you very much.

  • you need to model the database better. The ideal is to return all the data in just one query. Or you could create a view directly in the database. Think about it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.