Search code examples
phpcodeigniter-2sqlsrv

Trouble returning the affected rows on batch insert using SQLSRV


I am using codeignitor 2.1.4 and I inserting a bunch of rows using insert_batch. I am connected to SQL Server 2008 database using the SQLSRV driver. The query runs fine but when I run the affected_rows() function I am getting a weird result. Please note that I have made the change in the affected_rows function for the bug so that isn't the problem.

What I am seeing is the returned number being chopped off after 2 digits. So if the inserted rows were say 343 I would get 43. If the affected rows were 35312, I would get 12 returned.

I am wondering if anyone else has seen this type of behaviour? I have checked the return variable type and it is INT so its not that. I am lost as to where the error or change is taking place. Its almost like the return value is being taken as modulus 100 and returned.


Solution

  • So after looking around I found the oversight in the insert_batch and update_batch functions in Codeigniter.

    Because the inserts and updates are changed in 100 rows at a time the affected_rows function only sees the last ($rows%100) rows as that would be the last insert/update done.

    The solution I found was provided in this link:

    Affected rows solution

    It doesn't say it in the post there but you need to alter the insert_batch and update_batch functions in the system/database/DB_active_rec.php file.