quick mysql question
#1

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?
Reply
#2

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);
Reply
#3

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..
Reply
#4

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
Reply
#5

Yes, that worked. Thank you.
Reply
#6

No problem, glad I could help
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)