Current value in a sequence.

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

Current value in a sequence.

Post by swraman »

working in pgsql with PHP,

I have a table "tr_projects", it has a column "id", which are intigers.

What query/code do I have to run to get the last intiger from the column ID?

Can it be done with curval()? If so, what arguement do you use to retrieve it with curval?

Thanks
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Current value in a sequence.

Post by VladSun »

[sql]SELECT curval('sequence')[/sql]
is enough
There are 10 types of people in this world, those who understand binary and those who don't
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Current value in a sequence.

Post by swraman »

What is the arguement sequence?

I have the table name and the row name, but what is the sequence?

Thanks
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Current value in a sequence.

Post by VladSun »

You should set your id column is set by using nextval('sequence').
You have to create the sequence first (read the manuals).
Sequences are used for the same as auto_increment flag in MySQL.
There are 10 types of people in this world, those who understand binary and those who don't
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Current value in a sequence.

Post by swraman »

Can you link me to the documentation? Im having trouble finding documentation for a lot of these pgsql functions.
The code I had that I believe is faulty is:

public function GetInsertID($table, $column) {
$result = $this->Query("SELECT curval('$table_$column_seq')");
return $data[0];
}
I was given this code, I didnt write it. I think it is wrong because 1) it errors, and 2) there is no variable data in the function. Are my assumptions correct?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Current value in a sequence.

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Post Reply