Questions tagged [mysql-insert-id]

Retrieves the ID from the previous INSERT query on this database connection.

mysql_insert_id() is used to retrieve the ID from a previous INSERT query on this database connection.

The query must have been made to a table that has an auto_increment field - that is the field that will be returned.

You can pass in an optional parameter, which is the database connection to use; otherwise it will use the most recently opened database connection as a default.

Note that this function is connection-based - two separate processes invoking this function separately will create two separate connections to the database, which means that each process will return the correct ID for its own insert.

Note that as of PHP 5.5.0, this function has been deprecated.

124 questions
28
votes
4 answers

getting mysql_insert_id() while using ON DUPLICATE KEY UPDATE with PHP

I've found a few answers for this using mySQL alone, but I was hoping someone could show me a way to get the ID of the last inserted or updated row of a mysql DB when using PHP to handle the inserts/updates. Currently I have something like this,…
julio
  • 6,630
  • 15
  • 60
  • 82
13
votes
5 answers

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement'

When I run the following code, I get the error saying Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement' $mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There…
Hrishikesh Choudhari
  • 11,617
  • 18
  • 61
  • 74
13
votes
5 answers

How bad is using SELECT MAX(id) in MYSQL instead of mysql_insert_id() in PHP?

Background: I'm working on a system where the developers seem to be using a function which executes a MYSQL query like "SELECT MAX(id) AS id FROM TABLE" whenever they need to get the id of the LAST inserted row (the table having an auto_increment…
DrMHC
  • 795
  • 2
  • 11
  • 21
10
votes
4 answers

mysql_insert_id() returns 0

I know there are a lot of topics with the same title. But mostly it's the query that's been inserted in the wrong place. But I think I placed it right. So the problem is, that I still get 0 even when the data is inserted in the db. Does someone…
user750079
  • 159
  • 2
  • 2
  • 10
7
votes
1 answer

mysql_insert_id and last_insert_id wrong behavior

I have this table CREATE TABLE IF NOT EXISTS `t5` ( `id` int(11) NOT NULL auto_increment, `a` int(11) NOT NULL, `b` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `a` (`a`,`b`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ; a_b is a…
Matt.Z
  • 602
  • 7
  • 19
6
votes
6 answers

Alternative to "PDO::lastInsertId" / "mysql_insert_id"

I always hear that using "lastInsertId" (or mysql_insert_id() if you're not using PDO) is evil. In case of triggers it obviously is, because it could return something that's totally not the last ID that your INSERT created. $DB->exec("INSERT INTO…
BlaM
  • 28,465
  • 32
  • 91
  • 105
5
votes
3 answers

MYSQL if not exists insert data

Guys I'm getting duplicate records on insert into my database for some reason with this code $qry = "INSERT IGNORE INTO reports (". implode(", ",array_keys($reports)) .") VALUES (". implode(", ",array_values($reports))…
ehime
  • 8,025
  • 14
  • 51
  • 110
5
votes
1 answer

mysql_insert_id(); not returning value after successful row insert

I swear I have poured and poured over every other similar question on this site and others... but I think I'm just missing something. Hopefully someone can point out a silly mistake that my brain is hiding from me. :) My script inserts values from a…
Jake Garrison
  • 75
  • 1
  • 2
  • 6
5
votes
2 answers

Best way to find the last inserted ID in mysql using php

Im wondering as to what the best solution is to get the last inserted ID after a mysql inquiry? I have found the following solutions :
Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65
4
votes
2 answers

mysql_insert_id issue in concurrency data inserting

How reliable is mysql_insert_id() in a competitive condition? I mean when multiple users are inserting data at the same time, will this function return the true ID or it will return other user's inserted data ID? The table engine is MyISAM.
hd.
  • 17,596
  • 46
  • 115
  • 165
3
votes
1 answer

mysql_insert_id() stopped working after PHP update

I am preparing to update a live web server from PHP 5.2.12 to 5.3.5. In preparation, I have performed the update on a second server which is a mirror of the live one ("dev" server). Both servers are using FreeBSD, and both used ports to install…
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
3
votes
2 answers

Using mysql_insert_id to insert a lot of data

I want to use PHP and MySQL to insert data into a data table and its associated many-to-many table. The auto-incrementing primary ID of the data table is used in the many-to-many table. So, I need to know the ID's of the data table to insert into…
Ben G
  • 26,091
  • 34
  • 103
  • 170
3
votes
2 answers

php: mysql_insert_id() issues

Can the php function mysql_insert_id() return no result after processing the INSERT query in mysql db? Just to clarify. There was a script performing by cron on the production site. It contained a cycle for generating invoices for users. Each…
Andrey Pesoshin
  • 1,136
  • 1
  • 14
  • 30
3
votes
3 answers

Get insert_id for all rows inserted in single mysqli query (multiple values)

Working in PHP and using MYSQLI. Trying to get the insert_id for all rows inserted from a single insert query with multiple values. Keeping it in a single call for efficiency. My actual code has hundreds of values in one insert query. However, here…
Joe Bergevin
  • 3,158
  • 5
  • 26
  • 34
2
votes
3 answers

Getting auto-incremented int and inserting into another column

I'm trying to create a new row, in which the image name is the same as the recordID (plus '.png'). The recordID is an auto-incremented int. This makes a new record: mysql_query("INSERT INTO record (name,age) VALUES ('$name','$age'')"); ..and this…
cannyboy
  • 24,180
  • 40
  • 146
  • 252
1
2 3
8 9