如何获得pg_last_error的特定部分
问题描述:
我希望能够返回一个异常,并且php会这样做。但是,PHP也会返回特定异常的上下文,我不太喜欢前端用户看到。有什么方法可以显示错误部分而不是异常的上下文部分?如何获得pg_last_error的特定部分
$query3 = "INSERT INTO sis.enrolledsubject (offeringno, regno)
VALUES ('$offering','$regno')";
if (pg_query($query3)) {
echo "New subject added successfully!";
} else {
echo pg_last_error($dbconn);
}
的错误是这样的:
ERROR: //My exception string here//
CONTEXT: PL/pgSQL function resolveconflict() line 44 at RAISE
我只是想显示错误,而不是错误的情况下。我试过类似pg_result_error_field
但无济于事。我希望有人能帮助我。谢谢!
答
这里有一个SSCCE:
<?php
function error_exit($msg)
{
fwrite(STDERR, "ERROR: $msg\n");
exit(1);
}
$db = pg_connect("");
pg_query("create temporary table test(id int primary key)")
or error_exit(pg_last_error($db));
foreach (array_slice($argv, 1) as $arg) {
pg_send_query_params($db, "insert into test(id) values ($1)", [$arg])
or error_exit(pg_last_error($db));
$result = pg_get_result($db)
or error_exit(pg_last_error($db));
if (pg_result_status($result) == PGSQL_FATAL_ERROR)
error_exit(pg_result_error_field($result, PGSQL_DIAG_MESSAGE_PRIMARY));
}
?>
试试:
$ php test.php 1 2 2
ERROR: duplicate key value violates unique constraint "test_pkey"
$ php test.php 1 2 a
ERROR: invalid input syntax for integer: "a"
它说明了什么错误? –
类似这样:'错误://我的异常字符串在这里// 上下文:PL/pgSQL函数resolveconflict()第44行在RAISE' 我想只显示错误部分而不显示上下文部分 –
请参见: http://stackoverflow.com/questions/12349230/catching-errors-from-postgresql-to-php –