Skip to content
Advertisement

How to get affected row count on WordPress query?

This code is to use in WordPress plugin.

The following is my code I am using to insert data from CSV file to database:

JavaScript

When I do this var_dump($query); it shows int(0), and data is successfully inserted in table. My question is how can I get number of inserted rows?

Advertisement

Answer

Very old question, I know, and perhaps this answer is trivially obvious, but posting in the hope it might be useful to someone who stumbled across it as I did.

In this particular case with LOAD DATA, one option might be to simply run an sql COUNT() before and after the LOAD DATA, and then take the difference:

JavaScript

I understand this may not work well if there is other simultaneous activity on the table; though you may be able to lock the table to prevent conflicting updates and to make the LOAD DATA even faster. You might be able to get this with $wpdb->affected_rows and that’s also worth checking.

No doubt since it’s 2 years ago since you asked this, you have any number of other working solutions; hopefully this is useful to anyone googling for a solution here.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement