I'm using PDO inside a PHP script to do SQL queries. I want to do a transaction with more than one query and catch the errors. The code is like this:
try
{
$dbh = new PDO(...);
$dbh-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch
{
...
}
$query1 = 'DO some QUERY';
$query2 = 'DO other QUERY';
try
{
$dbh->beginTransaction();
$dbh->exec($query1);
$dbh->exec($query2);
$dbh->commit();
}
catch
{
...
}
How can I know which query caused the error? I want to do this because I want to ignore a duplicate key error for the first query but not for the second one, without using SELECT (see also a previous question of mine about UNIQUE error handling).