Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find the last ID inserted into a DB

Posted on 2013-12-02
2
Medium Priority
?
229 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 35

Accepted Solution

by:
gr8gonzo earned 2000 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 111

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses
Course of the Month7 days, 10 hours left to enroll

824 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