// 启动一个事务
query("begin work");
//查询下一个顾客号码
$res=query("select nextval('seq_customer_id')");
//检查错误
if (!$res || pg_numrows($res)<1) {
$feedback .= pg_errormessage($conn);
$feedback .= ' error - database didn't return next value ';
query("rollback");
return false;
} else {
$customer_id=pg_result($res,0,0);
// 登记到 session
session_register('customer_id');
// 插入新顾客
$res=query("insert into customers (customer_id)
values ('$customer_id')");
//检查错误
if (!$res || pg_cmdtuples($res)<1) {
$feedback .= pg_errormessage($conn);
$feedback .= ' error - couldn't insert new customer row ';
query("rollback");
return false;
} else {
//commit this transaction
query("commit");
return true;
}
}
}
?>
这段代码比较长,虽然我不是很喜欢,但是它演示了怎样正确开始和结束postgres 的事务以及怎样检查查询语句的错误。我要在所有的代码用到同样的错误监测程序,我想,你也应该如
| 对此文章发表了评论 |
