无效的PDO查询不会返回错误 [英] Invalid PDO query does not return an error
问题描述
下面的第二条SQL语句在phpMyAdmin中返回错误:
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说:
#1110 - Column 'bezeichnung_1' specified twice
全部正确.但是,当我使用此功能在Symfony 1.4中运行查询时:
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;
}
没有引发错误.这两个SQL语句彼此依赖,因此必须同时提交.错误的查询是根据用户输入构造的.我需要重新获得该错误,否则我无法确定数据库是否已更改,也无法告诉用户.
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.
您知道PDO为什么不抱怨无效语句吗?如果不能这样做,如何获取成功/失败信息?
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.
以下是PDOStatement类的链接: http://www.php .net/manual/en/class.pdostatement.php
Here's the link to the PDOStatement class: http://www.php.net/manual/en/class.pdostatement.php
推荐答案
这是预期的行为.由于有两个语句,第一个有效,因此您必须使用nextRowset()
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 */};
}
来源: bugs.php.net/bug.php?id=61613
这篇关于无效的PDO查询不会返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!