I had some Prepared Statements working in PHP using mysqli. The requirements changed and now I'm supposed to move them to the DB, as Stored Procedures. This worked fine for most of the PSs, except for a couple that read the insertId for some further processing. Ex:
$idAsdf = $stmtAsdf->insert_id;
where the PS performs an INSERT operation. I've tried using an OUT parameter on the procedure which works fine on PHPMyAdmin, but can't connect it with the PHP server code outside the DB. I haven't found any example of this combination of elements being done. How can I get this insertId using both SPs and PSs?
Thanks
For PDO Prepared Statement you can use PDO::lastInsertId -http://php.net/manual/en/pdo.lastinsertid.php
Just remember when using transaction return lastInsertId or store lastInsertId before commit.
For Stored Procedure - use LAST_INSERT_ID();
EDIT 1 :
If you using MySQLi - then use mysqli_insert_id - http://php.net/manual/en/mysqli.insert-id.php
If facing problem with out_param, use select to return last insert id as result.
EDIT 2 :
If you are facing problem in retrieving Stored Procedure result set use following code -
To access the out param use follwing code -
insert_id
is a property of mysqli class, while you are trying to get it from a statement object.inside SP you can set like this
outParam = LAST_INSERT_ID();
LAST_INSERT_ID()
returns the most recently generated ID is maintained in the server on a per-connection basis.