SQL returning empty, if($id == "" or $id == null or $id == 0) but only falls on Else

Asked

Viewed 49 times

0

Good afternoon guys, first thanks for helping me, I’m finishing a system and I have a problem that I believe is very simple, but I can not solve, I tried several ways to change the if, but will not

the code is this

public function verificavenda(){
    $c= new conectar();
    $conexao=$c->conexao();

    $sql="SELECT idvenda from vendas order by idvenda desc";

    $result=mysqli_query($conexao,$sql);

    $id=mysqli_fetch_row($result)[0];

    if($id == "" or $id == null or $id == 0){
        return 1;
    }else{
        return $id + 1;
    }
}

after the first $id created, it always falls on Isis, until then beauty, is what wanted

now when you have nothing in the database, give the following error > "trying to access array offset on value of type null"

and instead of falling into IF falls into Else in the same way, returning an error on the page instead of returning only the number

I’ve already lost a few hours, and I can’t find the train, thank you!

1 answer

0


Hello,

The error happens because you have no record in the database. Thus the function does not return an array but a primitive. What causes an error in trying to access index '0'.

Check if the version below helps you.

public function verificavenda(){
    $c= new conectar();
    $conexao=$c->conexao();

    $sql="SELECT idvenda from vendas order by idvenda desc";

    $result=mysqli_query($conexao,$sql);
    $row = mysqli_fetch_row($result);

    if(!$row){
        return 1;
    }
    $id = $row[0];

    return $id + 1;
}
  • good diaa worked perfectly, I will, but I would like to thank you even for having answered me the error, at least with this error I will not suffer more thank you my king!

  • Arrange... We are here to help. If you can mark the answer as accepted, thank you.

Browser other questions tagged

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