Tuesday, March 25, 2014

PDOException: Invalid text representation

Error:
The website encountered an unexpected error. Please try again later. [PDOException: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "as" LINE 5: WHERE (base.nid IN ('as')) ^: ..... (line 191 of/var/www/html/drupal/includes/entity.inc).]

Solution: 
Drupal throws PDO exception error like above while giving url patterns like,
localhost/drupal/node/abc
localhost/drupal/node/1.
localhost/drupal/node/1.23
localhost/drupal/node/<any special characters like *&^%$#@!>
This because of invalid input for select query while it is expecting integer. This is core level bug and the wrong input needs to be filtered in entity.inc of core module.
//add the below lines inside function load() in entity.inc.
//Patch to avoid non numeric node id's which causes pdo exceptions
if (is_array($ids)) {
        // Remove all non numeric ids.
        $ids = array_filter($ids, function($x){
                return preg_match('/^[0-9]+$/', $x);
        });
}

No comments:

Post a Comment