SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 09/02/2019, 05:05 PM   #1
ToiletDuck
Huge Clucker
 
ToiletDuck's Avatar
 
Join Date: Apr 2012
Location: Philippines, Lucena City
Posts: 400
Reputation: 43
Default Question about mysql_insert_id

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?
ToiletDuck is offline   Reply With Quote
Old 09/02/2019, 05:35 PM   #2
ComDuck
Big Clucker
 
ComDuck's Avatar
 
Join Date: Feb 2019
Posts: 82
Reputation: 12
Default Re: Question about mysql_insert_id

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.
ComDuck is offline   Reply With Quote
Old 09/02/2019, 05:53 PM   #3
ToiletDuck
Huge Clucker
 
ToiletDuck's Avatar
 
Join Date: Apr 2012
Location: Philippines, Lucena City
Posts: 400
Reputation: 43
Default Re: Question about mysql_insert_id

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?
ToiletDuck is offline   Reply With Quote
Old 09/02/2019, 06:21 PM   #4
ComDuck
Big Clucker
 
ComDuck's Avatar
 
Join Date: Feb 2019
Posts: 82
Reputation: 12
Default Re: Question about mysql_insert_id

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().
ComDuck is offline   Reply With Quote
Old 09/02/2019, 06:40 PM   #5
ToiletDuck
Huge Clucker
 
ToiletDuck's Avatar
 
Join Date: Apr 2012
Location: Philippines, Lucena City
Posts: 400
Reputation: 43
Default Re: Question about mysql_insert_id

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!
ToiletDuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql_insert_id question ToiletDuck Scripting Help 4 18/09/2017 09:20 AM
mysql_insert_id() OrangeJam Русский/Russian 1 07/01/2015 06:47 AM
mysql_insert_id issue DrDoom151 Scripting Help 5 18/08/2013 02:26 PM
last_insert_id and mysql_insert_id Zex Tan Scripting Help 3 17/08/2013 03:02 PM
mysql_insert_id return 0 CoaPsyFactor Scripting Help 4 06/06/2012 09:14 AM


All times are GMT. The time now is 07:22 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.