SA-MP Forums Archive
quick mysql question - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: quick mysql question (/showthread.php?tid=540871)



quick mysql question - TakeiT - 07.10.2014

I have autoincriment set in my database.

Say, I want to insert data into the table, then pull the ID. What is the best way to do this? I was hoping it automatically did SELECT like some plugins do..

Код:
mysql_format(con, query, sizeof(query),"INSERT INTO `projects` (`OWNER`, `NAME`) VALUES ('%s', '%s')", GetName(playerid), inputtext);
I would just select the id from projects where owner and name = ^, but if I can help it, I dont want either of those to be unique.

What's the best way to pull the id?


Re: quick mysql question - Chenko - 07.10.2014

If the ID column is auto incrementing you can use cache_insert_id()

The example from the wiki uses a threaded query but that isn't required. You can use an unthreaded query as well like so:

pawn Код:
new Cache:cache = mysql_query(con, "INSERT INTO mytable (myColumn) VALUES (myValue)");
new insertID = cache_insert_id();
cache_delete(cache);



Re: quick mysql question - Shaneisace - 07.10.2014

While your there i would highly suggest escaping the string inputs or you'll have someone try SQL inject or something.

Will also prevent people making changes the database etc..


Re: quick mysql question - TakeiT - 07.10.2014

Quote:
Originally Posted by Shaneisace
ПоÑмотреть Ñообщение
While your there i would highly suggest escaping the string inputs or you'll have someone try SQL inject or something.

Will also prevent people making changes the database etc..
I had it escaped, I made a spelling error in the query and retyped it, I guess I accidentally hit S instead of E :P

So i've never used cache, but that being said, I should be able to create a cache, run the query, parse it in a thread and then delete it. Thanks, i'll give it a try :P


Re: quick mysql question - TakeiT - 08.10.2014

Yes, that worked. Thank you.


Re: quick mysql question - Chenko - 08.10.2014

No problem, glad I could help