Page 1 of 1

Nextval()

Posted: Thu Jan 15, 2009 5:25 pm
by swraman
Okay, I need to get the nextval('sequence')

What exact code do I have to run to get the next value of the sequence?

Thanks

Re: Nextval()

Posted: Thu Jan 15, 2009 5:37 pm
by requinix
What is "sequence"? We aren't psychic, if we need to know something then you need to tell us.

Re: Nextval()

Posted: Thu Jan 15, 2009 5:40 pm
by Mark Baker
Assuming you're trying to access a Postgres sequence:

Code: Select all

SELECT nextval('sequence');

Re: Nextval()

Posted: Thu Jan 15, 2009 10:29 pm
by swraman
I ran that, and it gives me the result as something in the form "ResourceID=#"

Code: Select all

$result = pg_query("SELECT currval('".$table."_".$column."_seq')");
print_r($result);
outputs:

Code: Select all

Resource id #68
it doesnt seem to output the actual value of the current value, which is an intiger. Am I using it wrong still?

Re: Nextval()

Posted: Thu Jan 15, 2009 11:27 pm
by volomike
Database resources don't play like that. You have to do it like this:

Code: Select all

$rwRow = pg_fetch_array($result);
print_r($rwRow);
echo "<br />\n" . $rwRow['currval'];
And if you want nextval instead of currval, do it like so:

Code: Select all

$result = pg_query("SELECT nextval('".$table."_".$column."_seq')");
$rwRow = pg_fetch_array($result);
print_r($rwRow);
echo "<br />\n" . $rwRow['nextval'];

Re: Nextval()

Posted: Thu Jan 15, 2009 11:42 pm
by swraman
Thank you, I was missing pg_fetch_array.