SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 25/11/2015, 03:21 PM   #21
vassilis
High-roller
 
vassilis's Avatar
 
Join Date: Nov 2009
Location: Greece,Athens
Posts: 1,679
Reputation: 362
Default Re: Saving weapon data (MySQL)

So vince after reading it again and came to conclusion of how to use foreign keys and parental/child structuring. I have a small concern which i didn't actually get correctly in my brain. I can't actually understand how to get something from a field(Sorry if i speak bad English and you can't understand). Example:
I am making a House System and as you had told me in a post at scripting section
Quote:
House Owner should be linked to the player's unique id in the player table. A player's name is only to appear once in the entire database. Any other tables that require the player's information must link to the original table via a foreign key.
How could i set a player owner of a house or how to get if a player is owner of the house using id from player table?
What i did and i don't really think could work well is changing Username on my account table to Unique and then i setted a relation view through HOwner(variable for house owner name from houses table) with Username from account table. How could it work? In other words? How i could get the owner of the house(ID or name) using foreign key ?
vassilis is offline   Reply With Quote
Old 25/11/2015, 07:38 PM   #22
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,068
Reputation: 2658
Default Re: Saving weapon data (MySQL)

Like I said, the name should only appear once in the database. While it is technically possible to set up a foreign key relation with a text type field, it is discouraged to do so because string comparison is obviously much slower than integer comparison. Especially for varchar fields where length is not fixed.

If you have it properly set up you should have a column "ownedBy" or "ownerId" or something like that in the house table. That column only contains numeric ids, like 5 or 42. This id corresponds to ONE player. You can go and look this up in the player table to find out who it is.

Now, there's obviously also queries that do this. You can do this with two separate queries, or with a join. That depends on the situation.
PHP Code:
SELECT house.*, player.name 
FROM house
JOIN player
ON house
.ownerId player.id 
That will give all columns from the house table (house.*), plus an extra column which contains the player's name from the player table (player.name).
__________________
Vince is offline   Reply With Quote
Old 26/11/2015, 12:23 PM   #23
vassilis
High-roller
 
vassilis's Avatar
 
Join Date: Nov 2009
Location: Greece,Athens
Posts: 1,679
Reputation: 362
Default Re: Saving weapon data (MySQL)

Got it.

Last edited by vassilis; 27/11/2015 at 11:41 AM.
vassilis is offline   Reply With Quote
Old 05/03/2016, 01:47 PM   #24
vassilis
High-roller
 
vassilis's Avatar
 
Join Date: Nov 2009
Location: Greece,Athens
Posts: 1,679
Reputation: 362
Default Re: Saving weapon data (MySQL)

PHP Code:
public OnLoadPlayerWeapons(playerid)
{
    new
        
weaponid,
        
ammo;
    
    for(new 
icache_get_row_count(userDB); ji++) // loop through all the rows that were found
    
{
        
weaponid     cache_get_row_int(i0userDB);
        
ammo        cache_get_row_int(i1userDB);
        
        if(!(
<= weaponid <= 46)) // check if weapon is valid (should be)
        
{
            
printf("[info] Warning: OnLoadPlayerWeapons - Unknown weaponid '%d'. Skipping."weaponid);
            continue;
        }
        
        
GivePlayerWeapon(playeridweaponidammo); 
    }
    return;

Vince shouldn't this return a value at the end?
vassilis is offline   Reply With Quote
Old 05/03/2016, 01:53 PM   #25
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,944
Reputation: 504
Default Re: Saving weapon data (MySQL)

Quote:
Originally Posted by vassilis View Post
Aha i got it.. Seems good and readable good job vince.. i wonder if you would make something similar for y_ini it would really help me +rep though!
humm check my works u can get it for y ini

OT:Thanks Vince u r always helpful
SyS is offline   Reply With Quote
Old 05/03/2016, 02:11 PM   #26
vassilis
High-roller
 
vassilis's Avatar
 
Join Date: Nov 2009
Location: Greece,Athens
Posts: 1,679
Reputation: 362
Default Re: Saving weapon data (MySQL)

Quote:
Originally Posted by Sreyas View Post
humm check my works u can get it for y ini

OT:Thanks Vince u r always helpful
This post was 2 years ago i have learned to use MySQL mostly!
vassilis is offline   Reply With Quote
Old 05/03/2016, 02:12 PM   #27
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,944
Reputation: 504
Default Re: Saving weapon data (MySQL)

Quote:
Originally Posted by vassilis View Post
This post was 2 years ago i have learned to use MySQL mostly!
lol im sorry
SyS is offline   Reply With Quote
Old 08/05/2016, 02:22 AM   #28
vernz
Huge Clucker
 
vernz's Avatar
 
Join Date: Dec 2010
Posts: 230
Reputation: 6
Default Re: Saving weapon data (MySQL)

What do you do if you have a weapon in the slot 7, weaponid 35 by example and then you buy a weapon who use the same slot like weaponid 36, it will create a new weapon and leave the previous one in the table.. anything we could do for that ?
vernz is offline   Reply With Quote
Old 08/05/2016, 10:11 AM   #29
PrO.GameR
Gangsta
 
PrO.GameR's Avatar
 
Join Date: Oct 2012
Posts: 694
Reputation: 121
Default Re: Saving weapon data (MySQL)

Well as this topic is bumped I might aswell do it too, Vince why do you go to such long lengths to update a row on duplicates? Why don't you use REPLACE keyword? it acts like exactly like INSERT and if such unique ids exists it just updates them
__________________
Blueberry Prison Roleplay will be back soon!
Follow the forums for more information about opening day.

Forums
PrO.GameR is offline   Reply With Quote
Old 08/05/2016, 11:19 AM   #30
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,831
Reputation: 1401
Default Re: Saving weapon data (MySQL)

Quote:
Originally Posted by vernz View Post
What do you do if you have a weapon in the slot 7, weaponid 35 by example and then you buy a weapon who use the same slot like weaponid 36, it will create a new weapon and leave the previous one in the table.. anything we could do for that ?
As long as you execute a DELETE query before, it doesn't matter.

Quote:
Originally Posted by PrO.GameR View Post
Why don't you use REPLACE keyword? it acts like exactly like INSERT and if such unique ids exists it just updates them
http://stackoverflow.com/questions/9...168948#9168948
http://stackoverflow.com/questions/9...66919#27966919
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos 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
The problem with saving data in mysql. MaySee Scripting Help 3 22/07/2013 11:10 AM
MySQL saving Data yanir3 Scripting Help 5 24/06/2013 07:27 PM
Mysql Saving Data Stefand Scripting Help 7 07/05/2013 10:42 AM
Problems with MySQL data saving sobolanux Help Archive 0 22/04/2011 02:54 PM
Not saving to ini file (Weapon Data) Robbin237 Help Archive 5 02/05/2009 10:02 AM


All times are GMT. The time now is 09:28 PM.


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