get last inserted id, PostGreSQL
Posted: Mon Oct 30, 2006 9:15 am
In the section of code below, how can I get the last inserted id of the users.employees table. Thje DB is PostGreSQL. I saw the functions for MySQL, but is there anything similar that willl get the last inserted id within the current transaction for PGSQL? Thanks alot folks
if(!$found)
{
$db->query($sqlstrInsertUser);
$sUserAdded .= 'User Added' . '-' . $fname .' ' . $lname . '-' . $tax_id . '</br>';
}
if(!$found)
{
$db->query($sqlstrInsertUser);
$sUserAdded .= 'User Added' . '-' . $fname .' ' . $lname . '-' . $tax_id . '</br>';
}
Code: Select all
switch($action)
{
case "officials":
{
echo 'Here we will do lookup by both tax_id and email. If either exists, we will skip the record, if not we will insert';
echo '</br></br>';
$user = new Employee();
foreach ($_POST['chk'] as $name => $val)
{
$found = false;
$title = $_POST['title_' . $val];
$fname = $_POST['fname_' . $val];
$mname = $_POST['mname_' . $val];
$lname = $_POST['lname_' . $val];
$tax_id = $_POST['taxid_' . $val];
$username = $_POST['username_' . $val];
$password = $_POST['password_' . $val];
$suffix = $_POST['suffix_' . $val];
$imported_by = $_POST['inserted_' . $val];
if(isset($tax_id))
{
$sqlstrGetByTaxID = <<< SQL
SELECT
id
FROM
users.employees
WHERE
tax_id = '$tax_id'
SQL;
$sqlstrGetByUsername = <<< SQL
SELECT
id
FROM
users.employees
WHERE
username = '$username'
SQL;
$sqlstrInsertUser = <<< SQL
INSERT INTO
users.employees (username, pass, title, fname, mname, lname, tax_id, suffix, imported_by)
VALUES ('$username', '$password', '$title', '$fname', '$mname', '$lname', '$tax_id', '$suffix', '$imported_by');
SQL;
if($db->query($sqlstrGetByTaxID))
{
while($row = $db->fetch())
{
$found = true;
$sTaxIDExists .= 'User Tax ID Found' . '-' . $fname .' ' . $lname . '-' . $tax_id . '</br>';
}
if($db->query($sqlstrGetByUsername))
{
while($row = $db->fetch())
{
if(!$found)
{
$found = true;
$sUsernameExists .= 'Username Exists' . '-' . $username . '</br>';
}
}
}
}
if(!$found)
{
$db->query($sqlstrInsertUser);
$sUserAdded .= 'User Added' . '-' . $fname .' ' . $lname . '-' . $tax_id . '</br>';
}
}
}
echo $sUserAdded;
echo $sTaxIDExists;
echo $sUsernameExists;
break;
}
}