Connecting Tech Pros Worldwide Forums | Help | Site Map

Select last added number in postgres

Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#1: Aug 6 '09
When I insert a new data I want to check whether user already exist and also I want to take the last inserted record number and increment it by one. How can I check both conditions in one select statement? This is my code.
Expand|Select|Wrap|Line Numbers
  1. $result = pg_query($dbconn, "SELECT \"No\",\"Name\" FROM \"User\" ORDER BY \"No\" ASC;");
  2. while($row = pg_fetch_array($result)){
  3.     $Name=$row['Name'];
  4.      if($Name==$FullName)
  5.         {
  6.             echo "User already exist!";
  7.         }
  8. }
  9.  

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#2: Aug 6 '09

re: Select last added number in postgres


You don't do that last criteria in the same SQL, besides being next to impossible (yes you can do it with Unions and stuff, i know) but it's now how you do it.

You primary key field (the one you want to increment I assume) should be auto_increment. in mysql there is mysql_insert_id() which gives you the last insert in that session.

http://us2.php.net/manual/en/functio...-insert-id.php

postgres should have something similar, I just don't know it.

1. Check user doesn't exist
2. Do your insert
3. Get the last insert id (if you need it)

Dan
Reply


Similar PHP bytes