Condition in PHP "if" to know if the sent Mysql command worked or not

Asked

Viewed 149 times

2

I’d like to know what condition to put inside the if using PHP with Mysql database so it is possible to know if the command returned an error or not.

As seen in the code below, I want to delete a user from the database. However, I don’t know which condition to put on if so that he knows whether or not the command has worked.

Class Usuario
{
    private $pdo;
    public $msgErro="";
    public function conectar($nome,$host,$usuario,$senha)
    {
        global $pdo;
        try 
        {
            $pdo = new PDO("mysql:dbname=".$nome.";host=".$host,$usuario,$senha);
        } 
        catch (PDOException $e) 
        {
            $msgErro = $e->getMessage();
        }
    }
    public function deletar($email,$senha)
    {
        global $pdo;
        $sql = $pdo->prepare("DELETE FROM usuarios WHERE $email = :e AND $senha = :s");
        $sql->bindValue(":e",$email);
        $sql->bindValue(":s",md5($senha));
        $sql->execute();
        if ()
        {
            return true;
        }
        else
        {
            return false;
        }

    }

}

I just put the Class and the connect function to complement the code, but the problem is in the method deletar(). Note that after you have the command executed with $sql->execute(); there is in the code a if empty. I wonder what condition to put inside.

  • if ($sql->execute()) { //true }

  • $value = $sql->execute(); if($value){//true}Else{//false}

  • referent https://stackoverflow.com/questions/1661863/pdo-mysql-how-to-know-if-insert-was-successful

2 answers

6


A few remarks before you answer what you want. So some people can jump to the solution down there, that first part is only for those who want to do right and not just work.

Doesn’t make any sense to use that global $pdo. Avoid global as much as you can, and only use it if you know well what you are doing. In this particular case it is worse because you are doing something that is not global and has no need. In fact do not use any mechanism before mastering how it works, will prevent a lot of headache. This is especially important because this code has several problems using what you don’t know.

Your code probably suffers from SQL Injection. It may be that the die has been sanitized before, but I doubt, this is not the right way to do it. Its application will be vulnerable.

I do not know if I minimize saying not to worry, we see about 80% of the codes like this, because this is terrible, almost everyone is doing wrong. It’s not entirely the fault of the people who do it, but it’s a bit. Almost everywhere on the internet is teaching wrong. Right here on our site that we classify content happens a lot of this. At first it didn’t happen, we didn’t miss anything that taught wrong. Now there is a lot going on, in general most of the answers teach this wrong and the amount of problem is so great that we can’t even deal with everyone anymore, because most users don’t care if they are teaching wrong. It’s a little bit the fault of the person who gets the wrong information because they shouldn’t trust information on the Internet, this attitude will make them make mistakes all their lives induced by good or bad people. I know this has gotten a little long, off topic, but it’s the best I can teach you for everything in life.

The overwhelming majority of people adopt PDO for wrong reasons. I don’t know if I should adopt, even though it’s not the end of the world.

I don’t know if I should adopt classes, it’s little code for me to say, but it seems that this class is poorly structured, so using a mechanism the wrong way is not very good, and if you’re training, you’ll get used to doing wrong for the rest of your life. Connection mixing with the cases of what would be the user, seems very wrong. At least you didn’t inherit to make it worse, the solution might not be to run out.

It’s not about the code, but almost always deleting something from the database is wrong to do. Inactivation is usually better. In real codes this has vast consequences.

Anyway, I like to really help people, but this question is not about that it is not the case to go deep. The solution is always not to use what does not know how to do right.

Some people don’t like this kind of answer because I didn’t just focus on what was asked, but letting the person learn wrong is much worse. Sopt would be much more useful to people if all the answers were to make these alerts and we would have better codes, fewer questions would need to be closed.

The solution

Finally, not to mention minor problems, the most obvious solution to do what you want is to put at least the execute() within a try catch to catch the error since this option is turned on and already used in another point of the code. But there is an easier way:

return $sql->execute();

That’s all. If you look at the documentation, and should only use something after reading the documentation of that and fully understand, says that this function returns whether the operation was successful or not, which is exactly what you want.

Only this does not work well if you choose to receive the errors as exceptions.

Even if I wanted to check something it doesn’t make sense to use one if to deliver a value true or false, the if is already this value, is a redundant code.

  • Thanks for the answer. I’m right at the beginning, since it’s the first time I connect some php file with a database. I’m sure it should be totally out of the norms or standards. I’m just trying to learn. Thanks for the tips.

2

The execute method of the class PDOStatement returns a boleano, so you can check the result of this method in if. Follow the code:

 $execute = $sql->execute();

 if ($execute){
      return true;
 }else{
     return false;
 }

Browser other questions tagged

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