Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI 2.2.0 - pdo: rowCount non-object error because $this->result_id is not an object #3095

Closed
redjersey opened this issue Jun 9, 2014 · 5 comments

Comments

@redjersey
Copy link

After upgrading from 2.1.4 to 2.2.0 I have got an error:

Fatal error: Call to a member function rowCount() on a non-object in /var/www/system/database/drivers/pdo/pdo_result.php on line 47

Call Stack:
0.0001 130536 1. {main}() /var/www/index.php:0
0.0005 131856 2. require_once('/var/www/system/core/CodeIgniter.php') /var/www/index.php:217
0.0206 330348 3. CI_Controller->__construct() /var/www/application/controllers/login.php:9
0.0212 341972 4. CI_Loader->initialize() /var/www/system/core/Controller.php:51
0.0212 342252 5. CI_Loader->_ci_autoloader() /var/www/system/core/Loader.php:152
0.0279 645464 6. CI_Loader->library() /var/www/system/core/Loader.php:1178
0.0279 645564 7. CI_Loader->_ci_load_class() /var/www/system/core/Loader.php:216
0.0282 653940 8. CI_Loader->_ci_init_class() /var/www/system/core/Loader.php:975
0.0284 654496 9. CI_Session->__construct() /var/www/system/core/Loader.php:1099
0.0299 661100 10. CI_Session->sess_read() /var/www/system/libraries/Session.php:106
0.0326 664700 11. CI_DB_active_record->get() /var/www/system/libraries/Session.php:229
0.0327 665972 12. CI_DB_driver->query() /var/www/system/database/DB_active_rec.php:963
0.0338 681564 13. CI_DB_pdo_result->num_rows() /var/www/system/database/DB_driver.php:386

When I tried to trace $this->result_id in function num_rows (in pdo_result.php), $this->result_id is set to 1 and it's not an object (is_object($this->result_id) returns false)

@redjersey
Copy link
Author

I have traced and found out what caused the problem.

I have compared 2.2.0 pdo_driver.php vs 2.1.4 pdo_driver.php

the _execute() function of 2.2.0 is:

function _execute($sql)
{
    $sql = $this->_prep_query($sql);
    $result_id = $this->conn_id->prepare($sql);

    if (is_object($result_id) && ($result = $result_id->execute()))
    {
        if (is_numeric(stripos($sql, 'SELECT')))
        {
            $this->affect_rows = count($result_id->fetchAll());
        }
        else
        {
            $this->affect_rows = $result_id->rowCount();
        }
    }
    else
    {
        $this->affect_rows = 0;
        $result = FALSE;
    }

    return $result;
}

while the 2.1.4 _execute function is like this:

function _execute($sql)
{
    $sql = $this->_prep_query($sql);
    $result_id = $this->conn_id->prepare($sql);
    $result_id->execute();

    if (is_object($result_id))
    {
        if (is_numeric(stripos($sql, 'SELECT')))
        {
            $this->affect_rows = count($result_id->fetchAll());
            $result_id->execute();
        }
        else
        {
            $this->affect_rows = $result_id->rowCount();
        }
    }
    else
    {
        $this->affect_rows = 0;
    }

    return $result_id;
}

I'm not sure why 2.2.0 this function returns $result instead of $result_id,
$result = $result_id->execute() will only returns true of false:

http://www.php.net//manual/en/pdostatement.execute.php

this is why $this->result_id is set to 1 (true) and not the object.

narfbg added a commit that referenced this issue Jun 9, 2014
@narfbg
Copy link
Contributor

narfbg commented Jun 9, 2014

Please don't use PDO ... its driver is badly written and every time somebody wants to fix a bug, another one pops up. You don't get any benefit from using PDO anyway.

@marczellm
Copy link

narfbg, do you mean that your bug fix above introduces more bugs? And how should we should stop using PDO when there is no non-PDO driver to sqlite3 yet.

Although, I've found one of shady origins which does work:
[MODERATED: links to "shady origin" code]

Beware, there is some javascript at the end of the page.

Should these be added to CodeIgniter?

@narfbg
Copy link
Contributor

narfbg commented Jun 14, 2014

narfbg, do you mean that your bug fix above introduces more bugs?

No, but it is possible. This bug is the result of a "bug fix".

And how should we should stop using PDO when there is no non-PDO driver to sqlite3 yet.

Upgrade to 3.0-dev, it has a "native" sqlite3 driver and a sane PDO driver.

Although, I've found one of shady origins which does work:
,,,
Should these be added to CodeIgniter?

No, and I'm removing the links from your post, exactly because of the "shady origin" and because the docblocks inside claim that it's written by the ExpressionEngine Dev team, which is not true.

@Gremboloid
Copy link

in function _execute() of 2.2.0 lost reexecution

if (is_numeric(stripos($sql, 'SELECT')))
{
   $this->affect_rows = count($result_id->fetchAll());
   $result_id->execute();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants