2 mysql queries at once?
#1

So i am using SA-MP-MySQL R41-4, and when a player registers a new account, a query for adding a new row in the table of users is made by mysql_tquery function. Recently i added a system of inventory on my server, and now i need 2 queries for creating a row in the users table and in the inventories table to be executed at once, but it's impossible to do that in mysql_tquery function (i've tried separating them by ';', i get syntax errors in the mysql log). The only idea that i've got so far is to make "chain" of tquery'ies so that it first inserts a new row into talbe `users`, then in the callback of that tquery it inserts a new row into `inventories` table etc., but that's ultra ugly code, and there must be a much better way to solve my problem. So what can i do?
Reply
#2

What you did was right. Alternatively,

you could try MySQL stacked queries using the delimeter ";", so you can do multiple queries in one function call.

If you're using MySQLi checkout multi_query function.
Reply
#3

Just make 2 queries, it's simpler then you think.
PHP код:
mysql_tquery(handle"query 1");
mysql_tquery(handle" query 2"); 
Threaded queries are the best as they run on a separate thread and don't affect the main samp thread.
Reply
#4

The problem with 2 separate threaded queries is that i need to SELECT the data that i just inserted (inside of the callback of mysql_tquery), because i set default values (e.g. level: 1) in mysql table rather than hardcode it in the gamemode. I guess there is no cleaner way to do the thing other than hardcode default values for new-registered players and also keep default values in mysql tables? In that case all registered players' data is properly inserted into database and the same data is set on the server itself, and when a player logs in there must be 2 threaded queries (for `inventories` and for `players` tables) on whose callbacks all rows are inserted in appropriate variables on server, right?
Reply
#5

Quote:
Originally Posted by 0xAAAAAA
Посмотреть сообщение
The problem with 2 separate threaded queries is that i need to SELECT the data that i just inserted (inside of the callback of mysql_tquery), because i set default values (e.g. level: 1) in mysql table rather than hardcode it in the gamemode. I guess there is no cleaner way to do the thing other than hardcode default values for new-registered players and also keep default values in mysql tables? In that case all registered players' data is properly inserted into database and the same data is set on the server itself, and when a player logs in there must be 2 threaded queries (for `inventories` and for `players` tables) on whose callbacks all rows are inserted in appropriate variables on server, right?
If you set the default values from script you won't have to pull the player after inserting it.

No need to set default values from MySQL.
Reply
#6

When a player registers, isn't their inventory supposed to be empty? Insert a row only when an item is given to the said player.

If the server gives a few items to the player upon registration, you need to know their user ID but this does not mean a SELECT query!
pawn Код:
mysql_format(..., "INSERT INTO users ...", ...);
mysql_tquery(..., "OnPlayerRegister", "d", playerid);
pawn Код:
forward OnPlayerRegister(playerid);
public OnPlayerRegister(playerid)
{
    // store the user_id in a global array to use for reference
    // for this example, I will use a local variable
    new new_userid = cache_insert_id();

    // pass new_userid in the mysql_format below
    mysql_format(..., "INSERT INTO inventory ...", ...);
    mysql_tquery(...);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)