Solved

Find the last ID inserted into a DB

Posted on 2013-12-02
2
192 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now