MySQLi PHP: check if SQL INSERT query was completely successful using MySQLi

I have this great function that gets a lot of different data and inserts it into several tables. Not all data is always available, so not all SQL INSERT queries are successful. I need to check which SQL INSERT query was completely successful, and what was not done with such data (for example, inserting into a log table or the like).

Just to give you an example of how I think this can be done:

$sql = 'INSERT INTO data_table (ID, column1, column2) VALUES(?, ?, ?)'; if ($stmt->prepare($sql)) { $stmt->bind_param('iss', $varID, $var1, $var2); if ($stmt->execute()) { $success == TRUE; //or something like that } } 

I'm not quite sure if this is the best way, and if it always really shows if the data has been inserted into the table ... Any suggestions?

+4
source share
2 answers

From the PHP manual on mysqli_stmt::execute :

mysqli_stmt::execute - mysqli_stmt_execute - Executes a prepared query

Returns TRUE on success or FALSE failure.


 if ($stmt->execute()) { // exactly like this! $success = true; } 

You are doing it right ... What is your dilemma?

+11
source

I believe that you can use mysqli_stmt->affected_rows to return the number of rows inserted (either change or delete).

+2
source

Source: https://habr.com/ru/post/1310336/


All Articles