hgs
01-20-2010, 09:32 AM
Hi
Here we talk about tables with an id field of type auto increment.
Inserting a record into such a table assigns a new value to the id field, but how do you know about that value ?
Here is just another method to get that value.
The usage is pretty simple:
$newid=insert("mytable","myid","somefiled");
<?php
function insert($table,$idfield,$uuidfield)
/****************************************/
{
//////////////////////////////////////////////////////////////////////
//
// This function is for use with mysql.
// It is assumed that the table has a column if type 'auto increment'
// and a character field at least 36 charaters long
//
//////////////////////////////////////////////////////////////////////
$query="select max($idfield) from $table";
$result = mysql_query($query);
if (!$result) {
die("$query<br>" . mysql_error());
exit;
}
$row = mysql_fetch_row($result);
$idmax=$row[0]; // used later to speed up location of new record
//
// create a UUID
//
$myid=sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
//
// insert new record with this UUID
//
$query="INSERT INTO `$table` ( `$idfield` , `$uuidfield` ) VALUES (NULL , '$myid');";
$result = mysql_query($query);
if (!$result) {
die("$query<br>'". mysql_error());
exit;
}
//
// locate new record now using the UUID and the last auto increment value
//
$newid=$myid;
$query="Select $idfield from $table where $uuidfield='$myid' and $idfield>='$idmax'";
$result = mysql_query($query);
if (!$result) {
die("$query<br>'". mysql_error());
exit;
}
$num_rows = mysql_num_rows($result);
if($num_rows > 0) {
//
// clear the field that temporarily holds the UUID
//
$row = mysql_fetch_row($result);
$newid=$row[0];
$query="Update $table set $uuidfield='' where $idfield='$newid'";
$result = mysql_query($query);
if (!$result) {
die("$query<br>'". mysql_error());
exit;
}
} else {
//
// TROUBLE !!!
//
die('Could not find inserted record');
exit;
}
return $newid;
}
?>
Have fun !
Regards
Heinz
Here we talk about tables with an id field of type auto increment.
Inserting a record into such a table assigns a new value to the id field, but how do you know about that value ?
Here is just another method to get that value.
The usage is pretty simple:
$newid=insert("mytable","myid","somefiled");
<?php
function insert($table,$idfield,$uuidfield)
/****************************************/
{
//////////////////////////////////////////////////////////////////////
//
// This function is for use with mysql.
// It is assumed that the table has a column if type 'auto increment'
// and a character field at least 36 charaters long
//
//////////////////////////////////////////////////////////////////////
$query="select max($idfield) from $table";
$result = mysql_query($query);
if (!$result) {
die("$query<br>" . mysql_error());
exit;
}
$row = mysql_fetch_row($result);
$idmax=$row[0]; // used later to speed up location of new record
//
// create a UUID
//
$myid=sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
//
// insert new record with this UUID
//
$query="INSERT INTO `$table` ( `$idfield` , `$uuidfield` ) VALUES (NULL , '$myid');";
$result = mysql_query($query);
if (!$result) {
die("$query<br>'". mysql_error());
exit;
}
//
// locate new record now using the UUID and the last auto increment value
//
$newid=$myid;
$query="Select $idfield from $table where $uuidfield='$myid' and $idfield>='$idmax'";
$result = mysql_query($query);
if (!$result) {
die("$query<br>'". mysql_error());
exit;
}
$num_rows = mysql_num_rows($result);
if($num_rows > 0) {
//
// clear the field that temporarily holds the UUID
//
$row = mysql_fetch_row($result);
$newid=$row[0];
$query="Update $table set $uuidfield='' where $idfield='$newid'";
$result = mysql_query($query);
if (!$result) {
die("$query<br>'". mysql_error());
exit;
}
} else {
//
// TROUBLE !!!
//
die('Could not find inserted record');
exit;
}
return $newid;
}
?>
Have fun !
Regards
Heinz