Jump to content

fromJSON (SQL)


Recommended Posts

Posted

How to get arguments from the toJSON in fromJSON from the table the query sql?

local guns = toJSON ( { gun0=getPedWeapon(source,0),ammo0=getPedTotalAmmo(source,0), gun1=getPedWeapon(source,1),ammo1=getPedTotalAmmo(source,1) } ) 
local save = dbQuery( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", guns, getPlayerName(source) ) 
dbFree( save ) 
  

Now, how to retrieve data and to give the player weapons?

Posted

Since it's not magic, it doesn't matter how you stored the value, that's how programming works. To get the data from MySQL, use SELECT statement in dbQuery and then dbPoll to get the result.

Posted
How to get arguments from the toJSON in fromJSON from the table the query sql?
local guns = toJSON ( { gun0=getPedWeapon(source,0),ammo0=getPedTotalAmmo(source,0), gun1=getPedWeapon(source,1),ammo1=getPedTotalAmmo(source,1) } ) 
local save = dbQuery( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", guns, getPlayerName(source) ) 
dbFree( save ) 
  

Now, how to retrieve data and to give the player weapons?

  
local save = dbQuery( hconnect, "select Guns from users WHERE `Name`=?", getPlayerName(source) ) 
local poll = dbPoll( save, -1 ) 
local gunz = fromJSON( poll[1].Guns ) 
  

Posted (edited)

Guns are not issued, I use this:

local ld = dbQuery(hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username)  
local result = dbPoll(ld, -1 ) 
local gunz = fromJSON( result[1].Guns ) 
  
giveWeapon(source, gunz.gun0, gunz.ammo0) 

Edited by Guest
Posted
local guns = { } 
for slot = 0, 12 do 
    guns [ slot ] = 
        { 
            gun = getPedWeapon ( source, slot ), 
            ammo = getPedTotalAmmo ( source, slot ) 
        } 
end 
  
local save = dbExec ( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", toJSON ( guns ), getPlayerName ( source ) ) 

Posted
local guns = { } 
for slot = 0, 12 do 
    guns [ slot ] = 
        { 
            gun = getPedWeapon ( source, slot ), 
            ammo = getPedTotalAmmo ( source, slot ) 
        } 
end 
  
local save = dbExec ( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", toJSON ( guns ), getPlayerName ( source ) ) 

This is a insert and how to extract?

Posted
local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) 
local result = dbPoll ( ld, -1 ) 
local guns = fromJSON ( result [ 1 ].Guns ) 
if ( type ( guns ) == "table" ) then 
    for _, weapon in pairs ( guns ) do 
        if ( weapon.gun and weapon.ammo ) then 
            giveWeapon ( source, weapon.gun, weapon.ammo ) 
        end 
    end 
end 

Posted
local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) 
local result = dbPoll ( ld, -1 ) 
local guns = fromJSON ( result [ 1 ].Guns ) 
if ( type ( guns ) == "table" ) then 
    for _, weapon in pairs ( guns ) do 
        if ( weapon.gun and weapon.ammo ) then 
            giveWeapon ( source, weapon.gun, weapon.ammo ) 
        end 
    end 
end 

attempt to index local 'weapon' (a number value)

on:

if ( weapon.gun and weapon.ammo ) then 

Posted
Are you sure that it saved the way I gave you?

Yeah

Save:

local guns = { } 
        for slot = 0, 5 do 
            guns [ slot ] = 
            { 
                gun = getPedWeapon ( source, slot ), 
                ammo = getPedTotalAmmo ( source, slot ) 
            } 
        end  
        local save = dbExec ( hconnect, "UPDATE `users` SET `Guns`=? WHERE `Name`=?", toJSON ( guns ), getPlayerName ( source ) ) 
        dbFree( save ) 

Load:

local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) 
            local result = dbPoll ( ld, -1 ) 
            local guns = fromJSON ( result [ 1 ].Guns ) 
            if ( type ( guns ) == "table" ) then 
                for _, weapon in pairs ( guns ) do 
                    if ( weapon.gun and weapon.ammo ) then 
                        giveWeapon ( source, weapon.gun, weapon.ammo ) 
                    end 
                end 
            end 

Posted
Why you ignore castillo's post ? Yes we can help you?! I don't know what happens to people these days .

I'm sorry, I didn't see the 2nd page :)

SQL DB:

Guns:

[ { "ammo0": 1, "gun4": 32, "gun2": 22, "gun5": 30, "ammo3": 0, "gun3": 0, "gun0": 0, "gun1": 0, "ammo1": 0, "ammo5": 120, "ammo4": 64, "ammo2": 90 } ] 

Posted
That's not generated by the code I gave you.

[ { "1": { "ammo": 0, "gun": 0 }, "2": { "ammo": 90, "gun": 24 }, "3": { "ammo": 0, "gun": 0 }, "4": { "ammo": 0, "gun": 0 }, "5": { "ammo": 90, "gun": 30 }, "0": { "ammo": 1, "gun": 0 } } ] 

Now there is no error, but the weapon isn't issued

Posted
local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) 
local result = dbPoll ( ld, -1 ) 
local guns = fromJSON ( result [ 1 ].Guns ) 
if ( type ( guns ) == "table" ) then 
    for _, weapon in pairs ( guns ) do 
        if ( weapon.gun and weapon.ammo ) then 
            outputChatBox ( tostring ( weapon.gun ) ..", ".. tostring ( weapon.ammo ) ) 
            giveWeapon ( source, weapon.gun, weapon.ammo ) 
        else 
            outputChatBox ( "No weapon data" ) 
        end 
    end 
end 

See what it returns.

Posted
local ld = dbQuery ( hconnect, "SELECT `Guns` FROM `users` WHERE `Name`=?", username ) 
local result = dbPoll ( ld, -1 ) 
local guns = fromJSON ( result [ 1 ].Guns ) 
if ( type ( guns ) == "table" ) then 
    for _, weapon in pairs ( guns ) do 
        if ( weapon.gun and weapon.ammo ) then 
            outputChatBox ( tostring ( weapon.gun ) ..", ".. tostring ( weapon.ammo ) ) 
            giveWeapon ( source, weapon.gun, weapon.ammo ) 
        else 
            outputChatBox ( "No weapon data" ) 
        end 
    end 
end 

See what it returns.

[ { "1": { "ammo": 0, "gun": 0 }, "2": { "ammo": 90, "gun": 24 }, "3": { "ammo": 0, "gun": 0 }, "4": { "ammo": 0, "gun": 0 }, "5": { "ammo": 90, "gun": 30 }, "0": { "ammo": 1, "gun": 0 } } ] 

Output:

0, 0 
0, 1 
24, 90 
30, 90 
0, 0 

Posted
Well then, it has to work, these are the correct weapon ID's and ammo.

Are you sure that 'source' is the right player element?

Yes, I'm sure.

Maybe the problem ain't in this code?

It appears from other resources ain't issued weapons, too.

For example:

giveWeapon(source, 24, 500, true) 

What can be the reason?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...