how to select last autoincrement id using php and mysql [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php/MySQL insert row then get 'id'
I have an inset command such as
insert (name) values($name);
I have id column as autoincrement. How can I get the id of the inserted record after inserting.

insert (name) values($name);
SET #lastid = LAST_INSERT_ID();
select blah_blah from table where id = #lastid;
To get that back into php, you do a normal mysql select on it like this:
select #lastid;

For mysql you can use mysql_insert_id to get the id of the last inserted row.

Related

Get last generated id in MySql [duplicate]

This question already has answers here:
Get current AUTO_INCREMENT value for any table
(9 answers)
Closed 6 years ago.
I have an auto-increment column 'id' . Now I want to echo the last generated id.
I have tried these queries -
SELECT MAX(id) FROM table_name
SELECT id from table_name ORDER BY id DESC LIMIT 1
SELECT LAST_INSERT_ID();
Above two queries returning the Max Id which is actually the last id generated.
But the problem is suppose if I delete a row where id is 10, then again I insert a row. Then the id would be 11 in the last row, not 10. But these 2 queries are returning the value 9. But I want the value 10.
LAST_INSERT_ID() should be used after an insert query which I dont need to do.
How can I get the id value of 10 ?
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
This was taken from
Get current AUTO_INCREMENT value for any table

My SQL QUERY for insert into select statement gives error [duplicate]

This question already has answers here:
PHP, MySQL error: Column count doesn't match value count at row 1
(3 answers)
Closed 6 years ago.
$sql="INSERT INTO `tempahan`(`ic`,`nama`,`tarikh`,`tarikhakhir`,`mula`,`akhir`,`unit`,`bil`,`sebab`) ``SELECT ic, nama
FROM register";`
i use this to select the register column to be inserted in tempahan column.
but it gives me error 'Column count doesn't match value count at row 1'
just insert into the columns you really want to insert into - as the error states, the number of the columns need to be identical:
$sql="INSERT INTO `tempahan`(`ic`,`nama`) (SELECT `ic`, `nama`
FROM `register`)";
update: you can't use VALUES() when using a subquery to get the insert-values. I just corrected this.
Your insert statement for tempahan table provides more columns than you select from register table. Error message clearly says this. Use this:
$sql="INSERT INTO `tempahan`(`ic`,`nama`) ``SELECT ic, nama FROM register";`

How to get ID of each inserted row from one insert query using MySQLi module [duplicate]

This question already has answers here:
Get insert_id for all rows inserted in single mysqli query (multiple values)
(3 answers)
Closed 8 years ago.
I have this table structure:
TABLE test
id int(11) AUTO_INCREMENT
value1 text
value2 text
value3 int(11)
If I run this query:
SELECT value1,value2 FROM test WHERE value3=45
I'll get 2 rows as result.
I have this PHP code.
$stmt = $mysqli->prepare("INSERT INTO test (value1,value2) SELECT value1,value2 FROM test WHERE value3=45");
$stmt->execute();
$id = $stmt->insert_id;
$stmt->close();
echo $id;
The code will insert 2 rows because the selects gives 2 rows as result, right? But $id will only be the id of the first inserted row.
How could I get the id of the following inserted row?
If you always know how many inserted rows there are, you could just get the max id and de-increment by the number of rows.

a more relible solution to LAST_INSERT_ID [duplicate]

This question already has answers here:
Easy mysql question regarding primary keys and an insert
(4 answers)
Closed 9 years ago.
so I want to link two tables with a common column "messageID". so first I insert into table 1
to get the Auto incremented id, then take that ID with LAST_INSERT_ID function and give that as the id for table 2:
$db->("INSERT INTO table_1 VALUES('','$message')");
$db->("INSERT INTO table_2 VALUES(LAST_INSERT_ID(),'$message');
but here's my concern, there might be two users running this script simultaneously, so in the few milliseconds between the two queries exicuting the LAST_INSERT_ID could have changed, so now the two id's are different. Is there any way I can prevent this possibility. I know it is not possible to insert into two tables with one query, which was my first thoughts. Any ideas much appreciated. Thank you
The LAST_INSERT_ID is local to the connection session, so it will not conflict with a different user making an insert.
You could try using scope_identity, which would be something like this:
$db->("DECLARE #LAST_ID int
INSERT INTO table_1 VALUES('','$message')"
SET #LAST_ID = SCOPE_IDENTITY()
);
$arg = ... $myARR['LAST_ID'] ... //however you want to get the LAST_ID from your query to PHP here
$db->("INSERT INTO table_2 VALUES(#arg,'$message');
or
$db->("DECLARE #LAST_ID int
INSERT INTO table_1 VALUES('','$message')"
SET #LAST_ID = SCOPE_IDENTITY()
INSERT INTO table_2 VALUES(#LAST_ID,'$message')
);
LAST_ID would be the value of the Auto incremented id

INSERT with SELECT in MySQL + PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the id of a row i've just inserted php/mysql
I was wondering what's the most efficient way of inserting something and selecting it's ID property at the same time?
For example, I have a table with auto incrementing primary ID column. I insert an item into that table and I need to know the autoincremented ID for use with another table.
Right now, what I do is:
INSERT INTO table1 the data
SELECT FROM table1 the ID
INSERT INTO table2 another set of data along with ID.
You can do this
INSERT INTO foo (auto,text)
VALUES(NULL,'text'); --generate ID by inserting NULL
INSERT INTO foo2 (id,text)
VALUES(LAST_INSERT_ID(),'text'); --use ID in second table
src: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
You can try mysql_insert_id();
http://php.net/manual/en/function.mysql-insert-id.php

Categories