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.
if ($sql->execute()) { //true }
– user60252
$value = $sql->execute(); if($value){//true}Else{//false}
– user60252
referent https://stackoverflow.com/questions/1661863/pdo-mysql-how-to-know-if-insert-was-successful
– user60252