Uncaught PDOException with SQLSTATE[HY000]

Posted by:

Categories: Database|MySQL|PHP

19 Feb 2009

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);

You might also like :

1 Response to Uncaught PDOException with SQLSTATE[HY000]

Avatar

neha

October 4th, 2011 at 8:50 am

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).

Comment Form

About this blog

Based in South Africa, we're a web-development company...