This is not duplicated question.
The different thing is from other questions:
- My case is that it doesn't work sometimes without any exceptions.
I would like to know which part I need to check for this case.
My development environment is below:
- php7
- MySQL5.7
For sql process in php, I use PDO.
Here are snippets of source code.
$this->conn = new PDO('mysql:host='. $server. ';dbname='. $database, $username, $password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
try {
$this->conn->beginTransaction();
$total_amt = 100;
$sql = "INSERT INTO test_table (total_amt)"
." VALUES (:total_amt)";
$stmt_test = $this->conn->prepare($sql);
$stmt_test->bindParam (':total_amt', $total_amt, PDO::PARAM_INT);
$stmt_test->execute();
$inserted_id = $this->conn->lastInsertId();
$this->conn->commit();
} catch (PDOException $ex) {
$this->conn->rollBack();
exit();
} catch (Exception $e) {
$this->conn->rollBack();
exit();
} finally {
$closeDB(); // this is a function to close database connection
}
Mostly data inserted, but sometimes data was not inserted.
My question is... When it sometimes doesn't work, which part do I need to check?
I logged in the 'try' statement and 'catch' statement and the insert statement ran and I got the 'inserted_id' as well and no exceptions.
However when I check the data in the table, there is no the data.
If it always happen, I will understand, but sometimes it happens.
I am a newbie for php, if someone knows the reason, please let me know.