当我尝试将数据插入自定义表并且插入失败时,如何显示 mysql 抛出的错误?
例如,下面的一段代码应该(将会)因 SQL 错误而失败。
$insert = "some insert sql statement that will fail";
$myquery = $modx->query($insert);
    if(!$myquery){
        echo 'error occurred! <br>';
    }
Run Code Online (Sandbox Code Playgroud)
如何返回错误的实际情况[即列不匹配、唯一 ID 存在等]?
小智 5
有一种更简单的方法来跟踪您的自定义 xpdo 请求。
$c = $modx->newQuery('modResource');
$c->where(array(
    'id1' => 1
));
// print request for control
print_r($c->toSQL());
$s = $c->prepare();
$s->execute();
print_r($s->errorInfo());
Run Code Online (Sandbox Code Playgroud)
执行后我们可以捕获一个错误:
Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'modResource.id1' in 'where clause' )
Run Code Online (Sandbox Code Playgroud)
这都是因为 xpdo 使用 pdo 并在它的帮助下控制执行。来自 xpdo 源代码的一些代码:
/**
 * @see http://php.net/manual/en/function.pdo-errorinfo.php
 */
public function errorInfo() {
    if (!$this->connect()) {
        return false;
    }
    return $this->pdo->errorInfo();
}
Run Code Online (Sandbox Code Playgroud)