Timm Timm - 5 months ago 20
SQL Question

Invalid PDO query does not return an error

The second SQL statement below returns an error in phpMyAdmin:

SET @num=2000040;
INSERT INTO artikel( artikel_nr, lieferant_nr, bezeichnung_1, bezeichnung_1 )
SELECT @num := @num +1 AS anum, 70338, f2, f3
FROM import
WHERE id >1


MySQL says:

#1110 - Column 'bezeichnung_1' specified twice


All correct. But when I run the queries in Symfony 1.4 with this function:

// run sql query
// http://erisds.co.uk/symfony/snippet-creating-debugging-complex-sql-queries-in-symfony
// http://stackoverflow.com/questions/5434702/php-quick-refactoring
// param $sql: the query to run
// param $silent: bool if errors should be ignored
// throws: pdo error info if statement failed and $silent=false
// returns: pdo-statement (use for looping over result rows and error messages)
public static function runQuery($sql, $silent=false)
{
$conn = Propel::getConnection();
$pdo_statement = $conn->prepare($sql);

$error = null;
try
{
$pdo_statement->execute();
}
catch (Exception $e)
{
$error = $e->getMessage();
}

if ( !$error )
{
$pdo_error = $pdo_statement->errorInfo();
$error = $pdo_error[2];
}
if ( !$silent && $error ) throw new Exception($error);

return $pdo_statement;
}


no error is thrown. The two SQL statements must be submitted at the same time since they depend on each other. The faulty query is constructed from user input. I need to get that error back, otherwise I can't tell if the database was changed, and I can't tell the user about it.

Do you know why PDO doesn't complain about the invalid statement, and if it can't be made to do so, how to get the success/failure information?

BTW the query does update the database if there are no duplicate columns.

Here's the link to the PDOStatement class: http://www.php.net/manual/en/class.pdostatement.php

Answer

This is expected behavior. Since there are two statements and the first one is valid, you have to use nextRowset()

try
  {
    $pdo_statement->execute();
    while ($pdo_statement->nextRowset()) {/* https://bugs.php.net/bug.php?id=61613 */};
  }

Source: bugs.php.net/bug.php?id=61613

Comments