Possible Duplicate:
Retrieve (or simulate) full query from PDO prepared statement
Get Last Executed Query in PHP PDO
PDO Debugging - View Query AFTER Bind?
Using the PDOException
class, I can get various info relating to a SQL error. One part lacking is the actual SQL query which caused the error. This is helpful in troubleshooting integrity constraint violation errors. Even if the full query with inserted parameters is not available, the prepared query along with the array passed by execute()
would suffice as I can recreate the full query. Note that I currently do not use bindParam(), but pass all data using execute()
.
How can I view the actual query that caused the error? Thank you
Error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
website/accounts
, CONSTRAINTfk_accounts_zipcodes1
FOREIGN KEY (zipcodes_id
) REFERENCESzipcodes
(id
) ON DELETE NO ACTION ON UPDATE NO ACTION)
File Name: /var/www/main/components/com_upload/models/contacts.phpLine: 520
Time of Error: Monday October 15, 2012, 7:11:49 PDT
SQL which caused error:
INSERT INTO accounts (id, sites_id, name, address, cities_id, zipcodes_id, phone, fax, date_created, date_modified, record_status, vertical_markets_id,priority_id,roles_id) VALUES (:id,123,:name,:address,:cities_id,:zipcodes_id,:phone,:fax,NOW(),NOW(), "active" ,:vertical_markets_id,:priority_id,:roles_id)