-2

我在一个 PDO 准备语句中传递多个 INSERT 语句并且执行工作;但是,如果任何插入失败,则 execute() 始终返回 TRUE,并且永远不会调用 catch()。

$query = '
INSERT INTO resources SET
    resource_type_id = :audio_resource_type_id
    , resource_status_id = :resource_status_id
    , is_hosted = :is_hosted
    , category_id = :category_id
    , serve_url = :audio_serve_url
    , title = :title;

SET @audio_id = LAST_INSERT_ID();

INSERT INTO resources SET
    resource_type_id = :thumbnail_resource_type_id
    , resource_status_id = :resource_status_id
    , category_id = :category_id
    , serve_url = :thumbnail_serve_url
    , parent_resource_id = @audio_id;';

if ($audio_duration) {
    $query .= '     
    INSERT INTO audio_duration SET
    audio_id = @audio_id
    , duration_seconds = :audio_duration';
}

try {
    $sth = $dbi->dbh->prepare($query);
    $sth->bindParam('audio_resource_type_id', $this->resource_types['audio']);
    $sth->bindParam('thumbnail_resource_type_id', $this->resource_types['THUMBNAIL']);
    $sth->bindParam('resource_status_id', $resource_status_id);
    $sth->bindParam('category_id', $this->category_id);
    $sth->bindParam('audio_serve_url', $audio_serve_url);
    $sth->bindParam('thumbnail_serve_url', $thumbnail_serve_url);
    $sth->bindParam('title', $title);
    $sth->bindParam('audio_duration', $audio_duration);

    $sth->execute();

} catch (PDOException $e) {
    print "Error!: " . $e->getMessage();
    log($e->getMessage());
    alert($e->getMessage());
    return FALSE;
}

感谢您对此的任何想法。

4

1 回答 1

0

只想指出这一点。。

要阻止 PDO 静默失败,您可以在 PDO 连接上设置错误模式。

$dbh = new PDO();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

PDO::ERRMODE_WARNING如果你想要错误但仍然继续,还有。

于 2013-04-22T20:03:17.263 回答