Question about mysql_insert_id
#1

is it safe to use 'mysql_insert_id' for getting the last insert id?

for example there are two tables or one table 2 users did an insert query at once and then they get the last insert id in mysql using mysql_insert_id does this make some issue?
Reply
#2

mysql_insert_id is deprecated. If your query is an INSERT, then you use cache_insert_id.

Quote:

for example there are two tables or one table 2 users did an insert query at once...

You don't have to worry about it. The MySQL documentation states that the native will return the last AUTO_INCREMENT value based on a per-connection basis:

Quote:

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.

It is impossible for two different connections to collide with each other, as all clients will be sitting in their own unique connection to the server.
Reply
#3

Quote:
Originally Posted by ComDuck
View Post
mysql_insert_id is deprecated. If your query is an INSERT, then you
use cache_insert_id.



You don't have to worry about it. The MySQL documentation states that the native will return the last AUTO_INCREMENT value based on a per-connection basis:



It is impossible for two different connections to collide with each other, as all clients will be sitting in their own unique connection to the server.
Thank you!

What is the difference between
Code:
cache_insertid
and
Code:
mysq_insert_id
i have this code in my server
Code:
forward OnQueryInsertBaseObject(baseidx, objectid);
public OnQueryInsertBaseObject(baseidx, objectid)
	return BaseObjectData[baseidx][objectid][oSQLId] = mysql_insert_id(MainPipeline);
basically when the user execute a query to insert in the table this function is called and save the id into the variable
which is safer to use?
Reply
#4

Quote:
Originally Posted by ToiletDuck
View Post
What is the difference between
Code:
cache_insertid
and
Code:
mysq_insert_id
mysql_insert_id is completely deprecated and will never work with the latest MySQL release. If you do really want to know the difference, mysql_insert_id gets the ID generated for an AUTO_INCREMENT column by the latest sent query.

cache_insert_id works the same way as mysql_insert_id, except that it will not retrieve the ID if there isn't any active cache (you did not run threaded query or set an active cache manually) in the server. See the example on cache_insert_id in the SA-MP wiki. If you are interested with playing around and seeing which functions will lead to an active cache, use the cache_is_any_active native after you call something.

Quote:
Originally Posted by ToiletDuck
View Post
i have this code in my server
Code:
forward OnQueryInsertBaseObject(baseidx, objectid);
public OnQueryInsertBaseObject(baseidx, objectid)
	return BaseObjectData[baseidx][objectid][oSQLId] = mysql_insert_id(MainPipeline);
basically when the user execute a query to insert in the table this function is called and save the id into the variable
which is safer to use?
As I've said, mysql_insert_id is deprecated so avoid using it where possible. Use the latest plugin release, thread your queries, and switch to using a cached system. Then, use cache_insert_id() as a drop-in replacement to mysql_insert_id().
Reply
#5

Quote:
Originally Posted by ComDuck
View Post
mysql_insert_id is completely deprecated and will never work with the latest MySQL release. If you do really want to know the difference, mysql_insert_id gets the ID generated for an AUTO_INCREMENT column by the latest sent query.

cache_insert_id works the same way as mysql_insert_id, except that it will not retrieve the ID if there isn't any active cache (you did not run threaded query or set an active cache manually) in the server. See the example on cache_insert_id in the SA-MP wiki. If you are interested with playing around and seeing which functions will lead to an active cache, use the cache_is_any_active native after you call something.



As I've said, mysql_insert_id is deprecated so avoid using it where possible. Use the latest plugin release, thread your queries, and switch to using a cached system. Then, use cache_insert_id() as a drop-in replacement to mysql_insert_id().
Thank you!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)