PHP + mySQLi last id inserted [duplicate] - php

This question already has an answer here:
MYSQL last_insert_id() and concurrency
(1 answer)
Closed 8 years ago.
I know that mySQL has the query SELECT LAST_INSERT_ID().
This works properly well when it's a small site and not many users uses it. But, when your website has more users making use of it, there could be a problem with this select, because I can insert something at the same time as another one, so that could generate the problems.
I've been reading something from mySQLi Transactions, but I don't know much about it. Is this the only way to get the last id that I have inserted, not from other that can insert content at the same time as me??
Thanks in advance.

It won't be a problem, because last_insert_id() will get you only the last id inserted in the same session. You don't have to worry.
And as a note, it's just SELECT LAST_INSERT_ID();, not ...FROM table, you don't want to execute this function for every row in the table.
Read more about it here.

Related

mysqli_insert_id () [duplicate]

This question already has answers here:
mysqli_insert_id: What if someone inserts another row just before I call this?
(2 answers)
Closed 6 years ago.
is it safe to use mysqli_insert_id() when the database is heavily used and more than one id maybe inserted in the same time, or it will get only the id that inserted by a query from this user..
mysqli_insert_id() is specific to the database connection , it returns the ID of the row which you inserted most recently.

PHP MYSQL Select Last Inserted ID [duplicate]

This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 6 years ago.
I tried to find an answer on here already, as well as read the PHP doc on it, however it doesn't explain it very well.
I'm just trying to get the last ID inserted by the user. I know MYSQL is deprecated, but I need to do it this way.
This is what I have so far.
$query="INSERT INTO teams (team_name)
VALUES
('$team_name')";
mysql_query($query) or die('error');
$team_id = mysql_query("SELECT LAST_INSERT_ID()");
Thanks!
Take a look at mysql_insert_id
But beware of the issues when using ON DUPLICATE KEY UPDATE statements. It is described in the comments on the php doc page.
But your solution should work, too.
Also, if you're using mysql, don't forget to mysql_real_escape your input, $team_name in your case.
[insert obligatory mysql is deprecated warning here ;)]
You can do it by PHP. You don't need a query: mysql_insert_id()

How do I reference a row immediately after it has been added to a table? [duplicate]

This question already has answers here:
CodeIgniter activerecord, retrieve last insert id?
(11 answers)
Closed 8 years ago.
This is PHP / CodeIgniter / MySQL
The only way I can think of is to do the insert ($this->db->insert(...)), and then immediatly after, run another query to find the record again.
I'm hoping there is something (which seems a bit more efficient to me) that returns the primary key (or something) of the newly added record.
For codeigniter
$this->db->insert_id()
Have a look at here http://ellislab.com/codeigniter/user_guide/database/helpers.html and the first function is $this->db->insert_id();
This also works with activerecord inserts.
or you can do
$last_insert_id = $this->db->call_function('insert_id');

how i can get inserted id for sql server with php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get Insert id in MSSQL in PHP?
im working in project by php and sql server 2000, how i can get inserted id like mysql_inserted_id function which work with mysql .
im googled in the internet and cant find something...please leave example to how use it
You probably want to use SCOPE_IDENTITY() (which you can return via a SELECT statement) but the operator you use to get the latest ID depends on your implementation (your stored procedure may do more than one insert, for example, in which case ##IDENTITY would return only the latest ID). Check out this explanation:
http://www.sqlteam.com/article/alternatives-to-identity-in-sql-server-2000

How to get the id of a row i've just inserted php/mysql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I'm writing a function to add data do a database, but I then want to immediately use that entry in my code.
I know I could query: SELECT id FROM table ORDER BY id DESC
But I wonder if there's any more direct way of doing it.
That mostly depends on the interface you're using to access MySQL.
If you're using PDO (recommended), you'd use PDO::lastInsertId(): http://us.php.net/manual/en/pdo.lastinsertid.php
If you're using MySQL, you'd use mysql_insert_id() http://php.net/manual/en/function.mysql-insert-id.php
If you're using MySQLi, you'd use mysqli_insert_id(): http://us.php.net/manual/en/mysqli.insert-id.php
Just simply call whichever function is appropriate right after your INSERT completes.
It's important to note that you likely do not want to fetch id out of the database using SELECT because if you have multiple writers, you may get the id that some other thread inserted. At best, you'll operate on the wrong data, and at worst, you could have serious security issues and/or corruption.
With PHP you can use mysql_insert_id() function.
select last_insert_id();
Doc
Maybe this could help:
Get the Unique ID for the Last Inserted Row
If you insert a record into a table
that contains an AUTO_INCREMENT
column, you can obtain the value
stored into that column by calling the
mysql_insert_id() function.

Categories