Getting seperate tables[MYSQL]
#1

How i can get one value named ID at table1 and set it at table2 with same value
Example
if my table name is players and i have registered with name vassilis and automatically gets `ID` = 0 then
I want my player when he is buying a house to get thevalue from `players`/`ID` table/column and put the value to the second table named `houses` at column `HouseOwnerID` which is the same just in different table?
In other words how i can store one value from the first table to the second?
Reply
#2

I have tried to make a reference point for you, but don't take me as an expert - I'm rather the opposite with mysql.

PHP код:
new MySQL;
test(playerid)
{
    new 
query[128],
        
Name[MAX_PLAYER_NAME],
        
houseBeingEditedID 15// you use your own method of finding out which house is requiring editing
    
GetPlayerName(playeridName[playerid], MAX_PLAYER_NAME);
    
//---- find out player ID by name
    
mysql_format(MySQLquerysizeof(query), "SELECT * FROM `players` WHERE `name` = '%e' LIMIT 1"Name[playerid]);
    
mysql_tquery(MySQLquery);
    new 
localID cache_get_field_content_int(0"ID");
    
cache_delete(); // not necessary
    //---- localid = A_I'd player ID now
    
mysql_format(MySQLquerysizeof(query), "UPDATE `houses` SET `HouseOwnerID`=%i WHERE `ID`=%i"localIDhouseBeingEditedID);
    
mysql_tquery(MySQLquery);

Reply
#3

I was wondering about this a little while ago, i remember you could do something like:

pawn Код:
UPDATE `houses` SET `HouseOwnerID`= (SELECT `ID` FROM `players` WHERE `name` = '%s') WHERE `HouseID` = '%d'
I tried a similar query with my player/vehicle tables just then and it worked fine.

EDIT: forgot to mention that doing this will make this query take considerably longer than normal and should be avoided at all costs, as others have said its better to get the SQLID when the player connects and use from there.
Reply
#4

Quote:
Originally Posted by PinkFloydLover
Посмотреть сообщение
I was wondering about this a little while ago, i remember you could do something like:

pawn Код:
UPDATE `houses` SET `HouseOwnerID`= (SELECT `ID` FROM `players` WHERE `name` = '%s') WHERE `HouseID` = '%d'
I tried a similar query with my player/vehicle tables just then and it worked fine.
That's much better solution as it saves you some memory because you do it only at one query without using any variables.
Reply
#5

Why don't you retrieve the players MySQL ID when they log in and then save it into a variable?

Retrieve the row using their name as a reference and then use something like

PlayerSQLID[playerid] = cache_get_row_int(...
Reply
#6

Quote:
Originally Posted by PinkFloydLover
Посмотреть сообщение
I was wondering about this a little while ago, i remember you could do something like:

pawn Код:
UPDATE `houses` SET `HouseOwnerID`= (SELECT `ID` FROM `players` WHERE `name` = '%s') WHERE `HouseID` = '%d'
I tried a similar query with my player/vehicle tables just then and it worked fine.
I guess thats the best way..!
Reply
#7

Why don't you just fetch all the players info when they connect, so you can do something like this:

UPDATE `houses` SET `HouseOwnerID`=pInfo[playerid][userID] WHERE `HouseID`='%d'
Reply
#8

Quote:
Originally Posted by Dokins
Посмотреть сообщение
Why don't you retrieve the players MySQL ID when they log in and then save it into a variable?
That's how it should be done.
Reply
#9

Once you've stored it, you can manipulate it in any way.

To save it to the house owner it's simple, just do this:

pawn Код:
format(query, sizeof(query), "UPDATE `houses` SET `HouseOwnerID`= %d WHERE `HouseID` = %d, PlayerID[playerid], //houseid);
Also, don't use ' ' around %d as it's a fraction slower as it has to convert from a string to an integer!
(Just a tip).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)