how to prevent from getting wrong ID - php

database= mysql language=php
I am coding program to work like this
PC1 =computer1
step
1.PC1 insert data ,new ID from Auto-increment.
2.PC1 select last ID
everything work fine but..
The problem when your code is used by many computers at the same mili-sec.
For example
PC1insert data,Auto-increment new ID
2.PC2 insert data ,Auto-increment new ID
3.PC1 select last ID <-------Wrong
4PC2 select last ID
How to config database or modify php code to prevent this , thankyou.

You should use mysql_insert_id (or the equivalent call for the API you are using) to get the ID of the last inserted row.
I imagine that now you are doing this:
SELECT MAX(id) FROM yourtable
This won't work unless you use transactions. But using the function mysql_insert_id is the better way.
In the documenation there is also a caution and some notes that you should read:
Caution: mysql_insert_id() will convert the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT (64 bits) the conversion may result in an incorrect value. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query.
Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.
Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.

in mysql if you call
SELECT LAST_INSERT_ID();
you will get the last auto generated id fro the current connection, not for the whole server. So if another record is created in another connection it wont effect the result returned by LAST_INSERT_ID().

MySQL will return the correct last-inserted ID right after any INSERT statement per client (computer) connection, so you won't have to worry about that; the server won't mix them up. From the documentation:
Each client will receive the last inserted ID for the last statement that client executed.
As long as you call mysql_insert_id() in your PHP code to retrieve the insert ID, there's nothing to worry about.

mySQL has the LAST_INSERT_ID() function for that.
The PHP equivalent (if you use the classical mysql functions) is mysql_insert_id() with the exception that this function should be called immediately after the INSERT query because it acts on the last query made.
These functions work on a per-connection basis, it will not be influenced by records inserted by other clients.

Related

mysqli_insert_id() vs Stored procedure for insert in multiple tables [duplicate]

According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.

PDO lastInsertId: It may eventually fail? [duplicate]

According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.

Select last inserted rows with $mysqli->insert_id [duplicate]

According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.

How to display an incremental value that was just created in mysql

I have a PHP script that creates an entry into a mysql database. The PHP inserts all of the data except the primary key, which mysql automatically increments. The problem is that i want to insert information into two tables, and these tables must be able to associate. Is there a way to have PHP create an entry in one table in mysql, then figure out the incremental primary key value from that first table, in order for it to insert into the second mysql table as a reference?
Yes. This is certainly doable. The function/method you use to get the auto-incremented value that was just inserted will depend on the way you access MySQL from PHP. If you're using the mysql_ functions, use mysql_insert_id(). If you're using the mysqli_ functions (or OO versions), use mysqli_insert_id(). If you're using PDO, use PDO::lastInsertId(). In all cases, the function delegates to the MySQL function last_insert_id() which is local to the connection so you need not worry about concurrent threads interfering with each other.
You can use:
mysql_insert_id()
This will return the id of the previous query. See below:
http://php.net/manual/en/function.mysql-insert-id.php
To get the unique ID inserted for the first table you can use LAST_INSERT_ID() and save that as reference for your second table:
SELECT LAST_INSERT_ID();
Use this as another option I suppose. For PHP you can use mysql_insert_id as suggested :)

Is mysql_insert_id safe to use?

According to the PHP documentation, mysql_insert_id takes the last inserted id from the mysql table.
My question is, if I have a website that inserts more than 2 rows per second to the DB, can I use the mysql_insert_id and get the correct ID I am referring to in the INSERT query a line before?
From the MySQL manual:
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
Short version: it is safe to use.
mysql_insert_id gives you the insert id of the most recent INSERT statement on the connection you give it.
If you call this immediatly after your insert, on the same mysql connection, you get the inserted id matching that insert statement, independantly of any other inserts going on in the mean time.

Categories