This is one of those tiny little tips for “how to… ?” that I run into occasionally.
In PHPRunner (or ASPRunner!) events, like CustomAdd, where you are often doing more complex SQL processing, like inserting records into the current or related tables, you often need to know the ID of the record you just manually inserted in a CustomQuery(). I’m assuming that you are using AutoIncrement ID primary keys in this case.
For example, in an Employee Add process, in the customAdd event, I may have an additional step that inserts a row into a “user” table which is associated to the employee row.
$sql = "insert into user (field1, field2) values (somevalue, somevalue)"; CustomQuery($sql);
Now perhaps you need to set the userID column in the Employee record to the ID of the User record just inserted. How do you get that value?
Recall that all your database work is happening on a global object in PHPRunner called $conn.
If you declare that as global in your script (first line!) then you get access to $conn in your script.
global $conn; // rest of your script.
Now you can use regular MySQL functions to get the insert id out of $conn. Its a similar process for other supported databases.
all you need to code in your script is:-
$last_inserted_id = mysqli_insert_id($conn);
That’s it! Now you know the value of the autoincremented ID for the last inserted record that was done using $conn.