Welcome Guest, Not a member yet? Register   Sign In
SQLSRV num_rows fix
#1

[eluser]ranjudsokomora[/eluser]
Hello Fellow CI Coders,
I have modified the core db_driver.php so that you can use $query->num_rows() and it will return an accurate count of the rows. I did this by assigning the results of the query to the result_array [array], and checking the size of the array. Then I empty it so it looks like nothing happened.

Here is what my addition to [./system/database/DB_driver.php] the query function is:
Code:
else
{
    SWITCH ($this->dbdriver) {
      CASE "sqlsrv":
        $RES->num_rows = sizeof($RES->result_array());
        $this->result_array = array();
      BREAK;
    }
}

I am attaching the full file for reference.
#2

[eluser]ranjudsokomora[/eluser]
Good Day CI Coders,
I am not trying to be a pest, but could I get some feedback on this?
#3

[eluser]Unknown[/eluser]
It worked for me.
#4

[eluser]Unknown[/eluser]
I came across this issue in a similar way finding that it will always return 0 or FALSE.
I saw the change that you made but I really wanted it to work in a similar (if not identical) way to the other database drivers that CI provides. This is one reason why even though your fix will work I suggest a possible different fix.
Inside the sqlsrv_result.php file in the num_rows function, originally this is what it looks like:
Code:
function num_rows()
{
    return !$num_rows = sqlsrv_num_rows($this->result_id) ? 0 : $num_rows;
}
this will return 0 or a FALSE. I see that the $num_rows variable is not set but it seems that they want to use that variable to be the number of rows that the statement returns. The one thing that we need to remember when using this function is that the sqlsrv_num_rows function (per the documentation) will return FALSE if the cursor type is forward or static. Otherwise this will return a number.
So the change I made to do this is as follows:
Code:
function num_rows()
{
    return  sqlsrv_num_rows($this->result_id);
}
This will return the number of rows, or FALSE if the cursor is not the right kind. I would use this way before (and most of the time in place of) the way that you have suggested since it will use a database operation and not use much operations inside of php. This will share the processing (and memory capacity) with the database.




Theme © iAndrew 2016 - Forum software by © MyBB