Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_insert_id() and php 2

Status
Not open for further replies.

JJJ777

Programmer
Jun 5, 2002
33
MY
hi all

i have multiple adding form ..

so in order to return the id generated by auto increment field in the previous insert query i used
mysql_insert_id();

so what is it in postgresql?

thank in advance..
 
here's a quote from that explains it all:

Another problem you may run into when changing your code is the absence of a Postgres equivalent of MySQL's mysql_insert_id(), which returns the index value of the last INSERT query. The PHP documentation's language may mislead one to think that pg_getlastoid() does the job, but that is not the case. The lack of such a function is actually not a downside, for it is a result of Postgres's power in allowing multiple auto-incrementing fields through the SEQUENCE system.

Fortunately, getting the last ID is easy. Sequence information can be accessed through SQL, so the following replacement for mysql_insert_id() is possible:
Code:
function postg_insert_id($tablename, $fieldname)
{
  global connection_id;  
  $result=pg_exec($connection_id, "SELECT last_value FROM ${tablename}_${fieldname}_seq");  
  $seq_array=pg_fetch_row($result, 0);  
  return $seq_array[0];
}
-gerrygerry

Standard response to one of my posts:
"No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul."
 
I haven't used PostgreSQL much, but I would imagine that there's a gotcha in that advice: if you don't perform the insert and the return of the sequence id within a single transaction, you're not guaranteed to get the id of the record your process inserted.
______________________________________________________________________
TANSTAAFL!
 
Well... i've never used postgresql at all... lol... I just pulled that off a quick search! it just sounded educated... -gerrygerry

Standard response to one of my posts:
"No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul."
 
hi guys...
it seems doesn cari the id that autocreated bye prevoius insert ..therefore here i post the code for firs page which is page1.php. so my point is just to find the similar fuction as mysql_insert_id in postgresql..:)
*in page2.php i will put hidden field for the profileid..tq.


<?php
$dbconn = mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;) or die (&quot;Could not connect to database&quot;);
mysql_select_db(&quot;jjj&quot;) or die (&quot;could not open database&quot;);

if($Submit==&quot;Submit&quot;){

$query=mysql_query(&quot;insert into table1 (nama,ic) values ('$nama','$ic')&quot;) or die (&quot;could not select&quot;
//here is what i stuck in postgressql..
$profileid=mysql_insert_id();
header(&quot;Location: profileid=$profileid&quot;);
exit;
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;<?php echo $PHP_SELF; ?>&quot;>
<p>nama :
<input type=&quot;text&quot; name=&quot;nama&quot;>
</p>
<p>ic :
<input type=&quot;text&quot; name=&quot;ic&quot;>
</p>
<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</p>
</form>
</body>
</html>
<?php
mysql_close($dbconn) or die (&quot;could not close database&quot;);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top