如何使用FluentPDO

时间:2015-11-16 07:58:52

标签: php database pdo error-handling

我正在使用FluentPDO来构建我的查询。

执行insert语句时,如果无法执行,则返回False。但是,我无法弄清楚如何检索失败背后的错误。

有没有人知道如何在使用此库时处理错误?

这是我目前的代码......

function doInsert() {
    $pdo = new PDO("mysql:dbname=blog", "root", "password");
    $fpdo = new FluentPDO($pdo);

    $values = [
        'field1' => 'value 1',
        'field2' => 'value 2',
        'field3' => 'value 3',
    ];

    $query = $fpdo->insertInto('my_table', $values)->execute();
    if (!$query) {
        // what to type here to determine error
    }
}

1 个答案:

答案 0 :(得分:1)

这不是答案,而是作者的解释(FluentPDO / BaseQuery.php Line:98):

    // At this point, $result is a PDOStatement instance, or false.
    // PDO::prepare() does not reliably return errors. Some database drivers
    // do not support prepared statements, and PHP emulates them.  Postgres
    // does support prepared statements, but PHP does not call Postgres's
    // prepare function until we call PDOStatement::execute() (below).
    // If PDO::prepare() worked properly, this is where we would check
    // for prepare errors, such as invalid SQL.