SQL query in PHP

Asked

Viewed 45 times

1

I want to run this sql1 and if it does not return me any results I run sql2 and present it, but so it is not working, when sql1 returns result it presents right, but when there is no result, it is not presented the result of sql2. How should I do?

Thanks for the help. VLW!!!

public function listaConciliar($id) {
            //Conectar ao banco;
            $this -> conectar();

            $sql1 = "SELECT id_tipo, id_empresa, id_acao, cod_empresa, empresa, data_inicial, data_final, usuario, descricao FROM empresa AS e INNER JOIN tipo AS t ON e.id_empresa = t.empresa_id_empresa INNER join acao AS a ON a.id_acao = t.acao_id_acao where id_empresa = ".$id." and id_acao = 1 ORDER BY id_tipo DESC LIMIT 1;";

            $resultadoEmpresaPeriodo = $this -> conexao -> query($sql1);

            if ($resultadoEmpresaPeriodo != NULL) {

                $empresa = new Empresa();
                while ($row = $resultadoEmpresaPeriodo -> fetch_assoc()) {              
                    $empresa -> setIdEmpresa($row['id_empresa']);
                    $empresa -> setCodEmpresa($row['cod_empresa']);
                    $empresa -> setEmpresa($row['empresa']);
                    $empresa -> setDataInicial($row['data_inicial']);
                    $empresa -> setDataFinal($row['data_final']);
                    $empresa -> setUsuario($row['usuario']);
                }

                $this -> desconectar();

                return $empresa;
            } else {

                $this -> conectar();

                //Executar sql;
                $sql2 = "SELECT id_empresa, cod_empresa, empresa FROM empresa WHERE id_empresa = ".$id." ORDER BY empresa LIMIT 1;";

                $resultado = $this -> conexao -> query($sql2);

                $empresa1 = new Empresa();
                while ($row = $resultado -> fetch_assoc()) {                
                    $empresa1 -> setIdEmpresa($row['id_empresa']);
                    $empresa1 -> setCodEmpresa($row['cod_empresa']);
                    $empresa1 -> setEmpresa($row['empresa']);
                }

                $this -> desconectar();

                return $empresa1;
            }

            /*$this -> desconectar();

            return $empresa;*/

        }
  • Are you sure the company with the $id Does that function exist? Probably not, and both queries return empty so, and it only works when the first query has results, because it does exist, just as it would show if it ran the 2nd query, it just doesn’t enter the else.

  • var_dump($resultadoEmpresaPeriodo); returns what?

  • I was able to solve by changing the way of checking whether the result is null or not, I executed both query and stored the result of the two then do an if to check which has no value:if ($company -> getDataInicial() != NULL) { $this -> disconnect(); Return $company; } elseif ($company -> getDataInitil() == NULL) { $this -> disconnect(); Return $empresa1; }

  • If compared by the number of lines of the tb query would work.

No answers

Browser other questions tagged

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