Solved

Find the last ID inserted into a DB

Posted on 2013-12-02
2
207 Views
Last Modified: 2013-12-30
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?
0
Comment
Question by:rgranlund
2 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39691225
You're mixing your database extensions. You're using PDO to execute the query, but you're trying to use the raw MySQL extension to get the insert ID. Use PDO to get the last insert ID:

http://php.net/manual/en/pdo.lastinsertid.php
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39692177
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.
http://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
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question