Uncaught PDOException with SQLSTATE[HY000]

Problem:

I was setting up a database query using a PDOStatement, by looping through an array of values and binding each parameter seperately. But on executing the query, I got the following error:

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`places/places`, CONSTRAINT `places_ibfk_3` FOREIGN KEY (`login_id`) REFERENCES `login` (`id`) ON DELETE CASCADE)’ in G:….

Solution:

Instead of seperately binding each parameter to a variable name with bindParam, as in the following code:

foreach ($columns as $col) {
    $stmt->bindParam(":$col", $value);
}

load all the bindings into an associative array, like this:

foreach ($columns as $col) {
    $bind[":$col"] = $value;
}

and pass the array to the execute function:

$result = $stmt->execute($bind);

One Response to “Uncaught PDOException with SQLSTATE[HY000]”

  1. neha says:

    i was getting pdo exception error when i tried to migrate mysql db to oracle by oracle driver and dbtng migrator.

    PDOException: SQLSTATE[HY000]: General error: 904 OCIStmtExecute: ORA-00904: “IDENTIFIER”.”GET_FOR”: invalid identifier (ext\pdo_oci\oci_statement.c:146) in db_table_exists() (line 2730 of C:\wamp\www\usmlerx\includes\database\database.inc).

Leave a Reply