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
2
votes
1 answer

Cannot insert the value in database using php

As i'm trying to insert the value in database through php code but it didn't work and also it didn't give any error. Here is the code that i'd tried to execute but not succeeded.
Mishal Baig
  • 175
  • 1
  • 2
  • 10
2
votes
1 answer

MySQLi get last insert ID and pass it true function

In my I am trying to retrieve the last_insert (ID) from a query true calling a function here is where i call my function: $lead = $_leadh->addLead($lead_data, $call_data['number'], $user); $LeadID = $lead['id']; I am not getting any result? This is…
jhon dano
  • 660
  • 6
  • 23
2
votes
1 answer

Mysql Insert get ID then insert returned Id

When executing multiple MySQl statements, is there a way I can can get the last inserted id and insert it in another table? For example if a primary key is a foreign key in my second table. Table one user_id | username| ------------------- 1 …
sammyukavi
  • 1,501
  • 2
  • 23
  • 51
2
votes
2 answers

Mysqli insert id returning null

$S = "INSERT INTO ". TBD ." (NODE, AV, BV) VALUES ('15', '$name', '$email')"; $Q = $CONN->query($S); $M = $Q->insert_id; $M returns NULL not 0 The above script runs the query fine, but will not return the unique ID generated. The table,…
Andy Webb
  • 121
  • 1
  • 9
2
votes
7 answers

mysql_insert_id() echos 0

I am not the first to have this problem but the solutions of the others don't work. This script always returns 0 for mysql_insert_id(); I am using multiple primary keys. $sql = "INSERT INTO Produkte (Name,Price,Description,User) VALUES…
Deproblemify
  • 3,340
  • 5
  • 26
  • 41
2
votes
3 answers

php mysql_insert_id() possible conflict with multi user

We know that mysql_insert_id() will give the last inserted ID, but I am wondering if we could get a problem given the situation below. UserA -> call a function that will insert a ROW to a TABLE and get the inserted ID datetime of execution:…
Sam San
  • 6,567
  • 8
  • 33
  • 51
2
votes
3 answers

If the column name of primary ,auto increment field is not id , can $this->db->insert_id() function be used

I am new to Codeigniter.. So this is my table: request_id | login_name | login_password | reg_date | status Can I use $this->db->insert_id() to get last inserted id if the primary auto increment column name is request_id instead of id? Any help…
Arun Unnikrishnan
  • 2,339
  • 2
  • 25
  • 39
2
votes
1 answer

Grab last ID of Insert query

I need to get the story_id from the last insert. Ive tried using mysql_insert_id() but it doesn't seem to work with my statement. Here is the code: $stmt = $this->db->prepare("INSERT INTO active_stories (story_id, current_chapter, creator,…
mkral
  • 4,065
  • 4
  • 28
  • 53
1
vote
2 answers

PHP mysql_insert_id() for MySQL UUID() primary keys?

Is there some equivalent to PHP mysql_insert_id to fetch the last inserted UUID() primary key? (I always get 0. It works for auto_inc integers though)
ina
  • 19,167
  • 39
  • 122
  • 201
1
vote
5 answers

Get last ID and display it

I've been looking around but most of the tutorials are showing the mysql_insert_id() but it's on the same document. I'm wondering if there is a way where you can get the last id of a column and echo it out. $image = mysql_query("SELECT * FROM images…
Onz
  • 15
  • 1
  • 4
1
vote
2 answers

How to upsert two database tables with one SQL statement?

There are two database tables described in the following. The asterisk highlights the primary keys. +----------------+ +----------------+ | posts | | url_references | +----------------+ +----------------+ | id* | |…
JJD
  • 50,076
  • 60
  • 203
  • 339
1
vote
4 answers

What do you put in the parentheses of mysql_insert_id()?

I tried setting a variable like this: $test_id = mysql_insert_id($dbc); because I wanted to get the id of the last row inserted. I am able to connect to the database and insert the row but the next line (setting $test_id) it says this: supplied…
chromedude
  • 4,246
  • 16
  • 65
  • 96
1
vote
1 answer

MySQLi insert_id returns 0

I've been trying to fix this now for the past 3 hours and I'm having no luck - my tables all have auto increment fields and the row is being inserted correctly, and the connection isn't being closed because the other rows afterwards are also…
Keydose
  • 689
  • 1
  • 6
  • 15
1
vote
1 answer

Using a single form inserting data into two differen tables though needing to insert last auto id from the first insert into the next

I have a form I need to insert some information into one table. Then grab the auto increment id from that insert and use it in the next insert statement. I get it all to work except for the grabbing the last id. mysql_insert_id()?…
Droid646197
  • 65
  • 2
  • 2
  • 8
1
vote
3 answers

mysql_insert_id, does not return the last inserted id when i place it in a function

mysql_insert_id does not return the last inserted id when i place it inside a function. im kinda confused why it does not. here is my code: function addAlbum($artist,$album,$year,$genre) { $connection = mysql_connect(HOST,USER,PASS); $sql =…
Adamski
  • 5,769
  • 9
  • 32
  • 32
1
2
3
8 9