Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Find the last ID inserted into a DB

I have the following query:
$new_order = $pdo->prepare("INSERT INTO workorders (date_booked, shippers_full_name) VALUES (:start_date, :shippers_full_name)");
		//start a record counter so we can track how many records are updated
		
		if(isset($_POST['new_workorder'])) {
			 	try {
    			$new_order->execute();
				$workorder_id = mysql_insert_id();
				}  catch(PDOException $e) {
   					echo 'ERROR: ' . $e->getMessage();
					}
			} else {
				$workorder_id = $_POST['workorder_id'];
			}

Open in new window


At: $workorder_id = mysql_insert_id();
I'm trying to grab the last auto incremented id number inserted into the DB, so I can use it in another query.  What am I doing wrong here?
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This article will show you how to do these things, mapping the familiar but obsolete MySQL extension to the modern MySQLi and PDO.  It also includes an example of using MySQLi with prepared statements.  Make a search for "Use an Iterator to Escape the Data and Load the Table" and you will find the way to the last INSERT id.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

You may also find this reference helpful.
http://www.iconoun.com/mysql_mysqli_pdo_function_map.php