Home > database >  What is considered success when using execute?
What is considered success when using execute?

Time:09-04

I have a quick question, since I am not sure what is considered success when using execute.

$test_id=5;
$stmt = $this->conn->prepare("UPDATE test_db SET request_time=now() WHERE id = ?");
$stmt->bind_param("i",$test_id);
$result = $stmt->execute();

I know that execute returns 1 on success and 0 on failure. But what exactly is failure? Is it a failure if the query went through, but updated 0 rows?

I would test this myself, but I don't have an environment that I can use. Thanks in advance.

CodePudding user response:

Success is when there is no error.

And a failure is when your query is not executed at all because of error. So you don't really need the $result variable, because the error will reveal itself.

It means you don't need to write any code to test for success:

  • in case of success your code will just run further
  • in case of failure the error will be raised and it will be handled elsewhere

See my article on PHP error reporting for the in-depth explanation

  • Related