When mysql_fecth_assoc, don’t you think, it kills the rest of the code, how do you make it return something?

Asked

Viewed 53 times

-1

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

I wonder if you can 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));

        }
    }
  • 1

    First of all, the "viva*" of the first select is missing the point. There are other code problems, but to comment on them, you’d need to understand exactly what you want to do. If your IF depends on quantities, you have to parse num_rows (or affected_rows in other cases that generate changes), and not if fetch brought the first

  • Hello, the question of the code 'point' I have already corrected, I will explain from the beginning of the process, come on: I register a person who will travel in a table, and then register the amounts that she has been paying, but to delete this person I also need to erase the amounts paid by this person. then I’ll have to delete in two tables at once, so far so good, but when I need to delete only one this delete command for the two tables, not delete only one, and for that I created the query and the IF, and compare the value of the query with the $ID that comes in the 'Function'.

1 answer

0


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

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 is equal to 0", that is, when there are no records... enter this condition.

Don’t forget to erase these or die(mysqli_error($conexao)) when you put it into production.

  • 1

    thanks friend @Andreicoelho, once again you helped me, thank you very much, it worked, I had not thought of this situation, thanks, gave right, it happened that after the $result, it gave some problem and he did not execultava the rest qndo had no value, and so, and already does the validation within the if, and then one way or another he concludes. thank you very much.

  • Tranquil @Juniorvenancio!

Browser other questions tagged

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