When mysql_fecth_assoc, do not you think, it kills the rest of the code, how to make it return something?

-1

Hello, I have a problem with this function when deleting, I search for what will be compared, and I put two delete conditions, because I need it to delete two tables in one condition, and if there is nothing in the other table, delete only one, in my simplicity I used a if but the search when it does not find any results in the table, it does not execute the rest.

Can you help me?

function deleta_pessoa_pg($conexao,$id){
        $id_valor = "SELECT viva.* from viagens_valor as viva where viva.id_pessoa = '$id'";
        $resultado = mysqli_query($conexao, $id_valor) or die(mysqli_error($conexao));
        $listaitem = mysqli_fetch_assoc($resultado) or die(mysqli_error($conexao));     
        //echo $listaitem['id_pessoa'];


    if (!$listaitem['id_pessoa']) {
                //echo 'fala';
                $query2 = "DELETE FROM viagens WHERE id = '$id'";
                return mysqli_query($conexao, $query2) or die(mysqli_error($conexao));
    }else{
                //echo 'oi';
                $query = "DELETE v.*, vv.* FROM viagens as v left JOIN viagens_valor as vv ON vv.id_pessoa = v.id WHERE v.id = '$id'";
                return mysqli_query($conexao, $query) or die(mysqli_error($conexao));

        }
    }
    
asked by anonymous 28.04.2018 / 15:49

1 answer

0

As quoted by @Bacco you need to use a count of records to do this. Try to do this:

if (mysqli_num_rows($resultado) == 0) {
                //echo 'fala';
                $query2 = "DELETE FROM viagens WHERE id = '$id'";
                return mysqli_query($conexao, $query2) or die(mysqli_error($conexao));
    }

In the example above, it says: "If the number of records equals 0", that is, when there are no records ... enter this condition.

Do not forget to delete these or die(mysqli_error($conexao)) when putting it into production.

    
29.04.2018 / 18:34