This topic is locked

Obtain last record

4/7/2007 11:04:12 AM
PHPRunner General questions
K
karen price author

Hi,
Anyone knows how can I obtain the last added record of my database? Thanks in advance.

D
Dale 4/7/2007

Here's the snippet I use when adding a new customer.

I use it in the AFTER ADD events of the table I need to know the last created Id.
global $conn;

$str2 = "select LAST_INSERT_ID() from `customer`"; // Replace `customer` with your table name

$rs2 = db_query($str2,$conn);

$data2 = db_fetch_numarray($rs2);

$_SESSION["last_inserted_row"] = $data2[0];

return true;
Hope this helps

K
karen price author 4/9/2007

Thanks Dale.
I have tried fetching the last_insert_id() but it keeps returning a 0. Do you have any idea why that is the case? Thank you.

Here's the snippet I use when adding a new customer.

I use it in the AFTER ADD events of the table I need to know the last created Id.
global $conn;

$str2 = "select LAST_INSERT_ID() from `customer`"; // Replace `customer` with your table name

$rs2 = db_query($str2,$conn);

$data2 = db_fetch_numarray($rs2);

$_SESSION["last_inserted_row"] = $data2[0];

return true;
Hope this helps

K
karen price author 4/9/2007

This is the code I used to obtain the last record of my database. I have a AUTO_INCREMENT field called ID.
function BeforeAdd(&$values)

{
// Parameters:

// $values - Array object.

// Each field on the Add form represented as 'Field name'-'Field value' pair
//** Custom code ****

// put your custom code here

global $conn;

$str2 = "select top 1 * from quotation order by ID desc"; // Replace `customer` with your table name

$rs2 = db_query($str2,$conn);

$data2 = db_fetch_array($rs2);

$values["Qnumber"]=$data2["Qnumber"];
return true;
// return true if you like to proceed with adding new record

// return false in other case
}
However, there seems to be a problem in my sql query statement but I just can't see it. Anyone knows what I am doing wrong?

J
Jane 4/9/2007

Hi,
you can use LAST_INSERT_ID() function in the After record added event only.

Here is the correct code:

function AfterAdd()

{

global $conn;

$str2 = "select last_insert_id() from quotation";

$rs2 = db_query($str2,$conn);

$data2 = db_fetch_array($rs2);

echo "last inserted row is ".$data2[0];

}

K
karen price author 4/9/2007

Hi Jane,
Thanks for your most prompt response. I understand that After record added event is the correct place to add the event to find the last inserted record in the database.
However, I need to perform some formatting before adding the record to the database. Right now, I need to parse certain fields in the last record of the database before adding a new record. Can it be done?

Thank you for your help.

Hi,

you can use LAST_INSERT_ID() function in the After record added event only.

Here is the correct code:

J
Jane 4/10/2007

Hi,
to change field values of the last inserted record use Before record added event and $values array where all field values are stored.
To select ID of the last inserted record with LAST_INSERT_ID() function you can use only After record added event.