Solved

Find the last ID inserted into a DB

Posted on 2013-12-02
2
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

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 110

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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 article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 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