我希望能够返回一个异常,并且php会这样做。但是,php还会返回该特定异常的上下文,我不太喜欢前端用户看到的内容。有什么方法可以显示错误部分而不是异常的上下文部分吗?
$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
这样的东西,但无济于事。我希望有人可以帮助我。谢谢!
答案 0 :(得分:0)
这是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"