Arizala
Arizala

Reputation: 13

MySQL MULTIPLE INSERT get rows that fail?

I'm a beginner with PDO and MySQL, so here's my question :

How can I be sure that when performing an INSERT .. SELECT (or MULTIPLE INSERT) all data will be inserted in database?

I know there is a rowCount() function but the number of rows inserted is dynamic. The only way I see would be to make a SELECT count(*), and then compare it with the rowCount(), but I'm not sure I'm doing it the right way.

And if not all data were inserted, is it possible to get rows which didn't work?

Also, is it possible that a SELECT query fails and retrieves only a few part of the data? (ex : It must retrieve 1000 rows but due to some failure, it retrieves 700) Or it's all or nothing?

Thanks for the help.

Upvotes: 0

Views: 806

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157872

How can I be sure that when performing an INSERT .. SELECT (or MULTIPLE INSERT) all data will be inserted in database?

You should tell PDO to throw an exception in case of error and thus there will be a PHP error in case of a failed query.

I know there is a rowCount() function

Row count has nothing to do in your case.

And if not all data were inserted, is it possible to get rows which didn't work?

It is advised to redo all the successful yet queries instead. To do so you have to wrap your inserts in a transaction.

However, if you want to keep alll the previous inserts in place, you may wrap execute call in a try and catch operator and do whatever workaround inside.

Also, is it possible that a SELECT query fails and retrieves only a few part of the data?

No.

Upvotes: 1

Related Questions