Nextval()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Nextval()

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Nextval()

Post by requinix »

What is "sequence"? We aren't psychic, if we need to know something then you need to tell us.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Nextval()

Post by Mark Baker »

Assuming you're trying to access a Postgres sequence:

Code: Select all

SELECT nextval('sequence');
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Nextval()

Post 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?
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Nextval()

Post 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'];
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Nextval()

Post by swraman »

Thank you, I was missing pg_fetch_array.
Post Reply