views:

71

answers:

3

Hi,

I am trying to work with postgresql 'nextval' in PHP. How can I fill in the parenthesis in the third line in order to replace TXN_ID with the value of nextval('schemadb.audit_txn_seq')?

$DB->query("SELECT nextval('schemadb.audit_txn_seq')");
$DB->query('SET CONSTRAINTS ALL DEFERRED');
$DB->query('SELECT schemadb.undo_transaction(TXN_ID)');

Thanks!

A: 

Try:

$DB->query("SELECT nextval('schemadb.audit_txn_seq')");
$DB->query('SET CONSTRAINTS ALL DEFERRED');
$DB->query("SELECT schemadb.undo_transaction( currval('schemadb.audit_txn_seq') )");
Justin Ethier
That doesn't seem to be working. Is there anyway to store the result of "$DB->query("SELECT nextval('schemadb.audit_txn_seq')");" into a PHP variable?
behrk2
Let's take a step back. What exactly are you trying to do? Do you just want to get the ID of the next value of the sequence?
Justin Ethier
I want to get the ID of the currval in the sequence. What you posted before didn't work, so I'm trying to see if I can store the currval of the sequence in a php variable. Thanks!
behrk2
+1  A: 

You did not tell us what is $DB variable. I guess you are using PDO.

The question "Is there anyway to store the result of query into php variable" is kind of beginner question, and is easilly answered in the manual.

You must understand, that (from PHP's point of view) there is no difference between

SELECT nextval('schemadb.audit_txn_seq')

and

SELECT xcolumn FROM xtable LIMIT 1

If you want to fetch data values from ANY query, you do it always the same, standard way:

  1. query
  2. execute
  3. fetch

Hope that helps.

filiprem
A: 

I'm not sure about the interaction with SET CONSTRAINTS ALL DEFERRED, but have you tried doing this:

SELECT schemadb.undo_transaction(nextval('schemadb.audit_txn_seq'))

The question is now what does "undo_transaction" return? If it returns the transaction ID upon successful completion, then you're good to go!

Matthew Wood