This code is to use in WordPress plugin.
The following is my code I am using to insert data from CSV file to database:
$sql="LOAD DATA LOCAL INFILE '".$fileurl."' INTO TABLE ".$table_name." FIELDS TERMINATED BY ',' LINES TERMINATED BY 'rn' IGNORE 1 LINES (`first_name`,`last_name`, `email`, `mobile_phone`, `address_1`, `address_2`, `city`, `state`, `zip`, `about_us` ); "; $query = $wpdb->query($sql);
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:
$count_before = $wpdb->query("SELECT COUNT(*) FROM $table_name"); // LOAD DATA ... $count_after = $wpdb->query("SELECT COUNT(*) FROM $table_name"); $new_rows = $count_after - $count_before;
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.