php里面不知道有没有打开事务的专用方法,如果没有,也可用SQL语句来打开,看一下手册http://www.postgresql.org/docs/view.php?version=7.3&idoc=0&file=sql-begin.html

解决方案 »

  1.   

    下面代码仔细看看,我想你应该清楚了!
    function query($sql) {
    global $conn;
    return pg_exec($conn,$sql);
    } query("BEGIN WORK"); //query postgres for the next value in our sequence
    $res=query("SELECT nextval('seq_customer_id')"); //check for errors
    if (!$res || pg_numrows($res)<1) {
    $feedback .= pg_errormessage($conn);
    $feedback .= ' Error - Database didn\'t return next value ';
    query("ROLLBACK");
    return false;
    } else {
    //set that value in a local var
    $customer_id=pg_result($res,0,0); //register the id with PHP4
    session_register('customer_id'); //insert the new customer row
    $res=query("INSERT INTO customers (customer_id) VALUES ('$customer_id')"); //check for errors
    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");