Jump to content

[HELP] Get SQL Account Datas


Ranous

Recommended Posts

Datas in database and panel datas is not equal.

Client-Side;

function getGridPlayer(userMoney,userKills,userDeaths,userJoins) 
    if ( guiGridListGetSelectedItem ( playersGridList ) ) then 
        local thePlayerName = guiGridListGetSelectedItemText ( playersGridList ) 
        getUserName = getPlayerFromPartialName (thePlayerName) 
        guiSetText (nameLabel,"Username: "..thePlayerName) 
        triggerServerEvent("onClientRequestAccountNames", getLocalPlayer(),getUserName) 
        guiSetText (moneyLabel,"Money: "..userMoney) 
        guiSetText (killsLabel,"Kills: "..userKills) 
        guiSetText (deathsLabel,"Deaths: "..userDeaths) 
        guiSetText (joinLabel,"Joins: "..userJoins.."x") 
    end  
end 
addEvent("onSendAccountNamesToClient", true) 
addEventHandler("onSendAccountNamesToClient", getRootElement(), getGridPlayer) 
addEventHandler ("onClientGUIClick",root,getGridPlayer) 

Server-Side;

function gAccountsToSend(getUserName) 
    local gAccount = getPlayerAccount(getUserName) 
    local gAccountName = getAccountName(gAccount) 
    if (gAccountName) then 
        local query = dbQuery (dataBase,"SELECT money,playerKills,playerDeaths,playerJoin FROM userDatabase WHERE accountName = ?",gAccountName) 
        local result = dbPoll (query,-1) 
        if (#result ~= 0) then 
            userMoney = result.money 
            userKills = result.playerKills 
            userDeaths = result.playerDeaths 
            userJoins = result.playerJoin 
        triggerClientEvent(source, "onSendAccountNamesToClient", getRootElement(), userMoney,userKills,userDeaths,userJoins) 
        end 
    end 
end 
addEvent("onClientRequestAccountNames", true) 
addEventHandler("onClientRequestAccountNames", getRootElement(), gAccountsToSend) 

Database;

PF8ajGM.png

Panel;

cKOGAc5.png

Debug Code;

ufYMaL8.png

Edited by Guest
Link to comment

Client

function getGridPlayer(userMoney,userKills,userDeaths,userJoins) 
    if ( guiGridListGetSelectedItem ( playersGridList ) ) then 
        local thePlayerName = guiGridListGetSelectedItemText ( playersGridList ) 
        getUserName = getPlayerFromPartialName (thePlayerName) 
        guiSetText (nameLabel,"Username: "..thePlayerName) 
        triggerServerEvent("onClientRequestAccountNames", getLocalPlayer(),getUserName) 
        guiSetText (moneyLabel,"Money: "..(userMoney or 0) ) 
        guiSetText (killsLabel,"Kills: "..(userKills or 0) ) 
        guiSetText (deathsLabel,"Deaths: "..(userDeaths or 0) ) 
        guiSetText (joinLabel,"Joins: "..(userJoins or 0).."x") 
    end 
end 
addEvent("onSendAccountNamesToClient", true) 
addEventHandler("onSendAccountNamesToClient", getRootElement(), getGridPlayer) 
addEventHandler ("onClientGUIClick",root,getGridPlayer) 
  

Link to comment

Remove your client code by this code and open your panel be sure the debugscript is actived before you open the panel and show us what's on the debugscript.

Code:

function getGridPlayer(userMoney,userKills,userDeaths,userJoins) 
    if ( guiGridListGetSelectedItem ( playersGridList ) ) then 
        local thePlayerName = guiGridListGetSelectedItemText ( playersGridList ) 
        getUserName = getPlayerFromPartialName (thePlayerName) 
        guiSetText (nameLabel,"Username: "..thePlayerName) 
        outputDebugString ( "UserMoney: ".. tostring ( userMoney ) )  
        outputDebugString ( "UserKills: ".. tostring ( userKills ) ) 
        outputDebugString ( "UserDeaths: ".. tostring ( userDeaths ) ) 
        outputDebugString ( "UserJoins: ".. tostring ( userJoins ) ) 
        triggerServerEvent("onClientRequestAccountNames", getLocalPlayer(),getUserName) 
        guiSetText (moneyLabel,"Money: "..(userMoney or 0) ) 
        guiSetText (killsLabel,"Kills: "..(userKills or 0) ) 
        guiSetText (deathsLabel,"Deaths: "..(userDeaths or 0) ) 
        guiSetText (joinLabel,"Joins: "..(userJoins or 0).."x") 
    end 
end 
addEvent("onSendAccountNamesToClient", true) 
addEventHandler("onSendAccountNamesToClient", getRootElement(), getGridPlayer) 
addEventHandler ("onClientGUIClick",root,getGridPlayer) 

Link to comment

Well,

I noticed that, the problem isn't in client side it's in server side

Because with this line, as far as userMoney variable is a nil value it will keep outputting number 0 to your label.

guiSetText (moneyLabel,"Money: "..(userMoney or 0) ) 

Link to comment

Full:Server-Side;

-- CONNECT DATABASE -- 
addEventHandler ("onResourceStart",root, 
function() 
    dataBase = dbConnect ("sqlite","userDatabase.db") 
    userDatabase = dbExec (dataBase ,"CREATE TABLE IF NOT EXISTS userDatabase(accountName TEXT,money INT,playerKills INT,playerDeaths INT,playerJoin INT)" ) 
end 
) 
-- ADD ACCOUNT DATA -- 
addEventHandler ("onPlayerLogin",root, 
function(_,acName) 
    local userAccountName = getAccountName (acName) 
    local accountNameQuery = dbQuery(dataBase, "SELECT accountName FROM userDatabase WHERE accountName = ?", userAccountName) 
    local result = dbPoll(accountNameQuery, -1) 
    if #result == 0 or not result then 
        createData = dbExec (dataBase,"INSERT INTO userDatabase(accountName,money,playerKills,playerDeaths,playerJoin) VALUES (?,?,?,?,?)",userAccountName,0,0,0,0) 
        if (createData) then 
            outputChatBox ("#FF8600*#C1C1C1Your account data was successfully created.",source,255,255,255,true) 
        end 
    end 
end 
) 
-- DELETE ACCOUNT DATA -- 
addCommandHandler ("deleteAccountData", 
function(thePlayer,command,accName) 
    local aName = getAccountName( getPlayerAccount(thePlayer) ) 
    if isObjectInACLGroup("user."..aName, aclGetGroup("Admin")) then 
        if accName then 
            local accountNameDB = dbQuery(dataBase, "SELECT accountName FROM userDatabase WHERE accountName = ?", accName) 
            local result = dbPoll(accountNameDB, -1) 
            if #result ~= 0 then 
                deleteUserData = dbExec (dataBase,"DELETE FROM userDatabase WHERE  accountName=?", accName) 
                if (deleteUserData) then 
                    outputChatBox ("#FF8600*#C1C1C1Account data was deleted. ("..accName..")",source,255,255,255,true) 
                end 
            else 
                outputChatBox ("#FF8600*#C1C1C1Account name could not be found. ("..accName..")",source,255,255,255,true) 
            end 
        end 
    end 
end 
) 
-- ACCOUNT CLIENT-SIDE -- 
function gAccountsToSend(getUserName) 
    local gAccount = getPlayerAccount(getUserName) 
    local gAccountName = getAccountName(gAccount) 
    if (gAccountName) then 
        local query = dbQuery (dataBase,"SELECT money,playerKills,playerDeaths,playerJoin FROM userDatabase WHERE accountName = ?",gAccountName) 
        local result = dbPoll (query,-1) 
        if (#result ~= 0) then 
            userMoney = result.money 
            userKills = result.playerKills 
            userDeaths = result.playerDeaths 
            userJoins = result.playerJoin 
        triggerClientEvent(source, "onSendAccountNamesToClient", getRootElement(), userMoney,userKills,userDeaths,userJoins) 
        end 
    end 
end 
addEvent("onClientRequestAccountNames", true) 
addEventHandler("onClientRequestAccountNames", getRootElement(), gAccountsToSend) 
-- STATICS: DEATHS -- 
function deathPlayer() 
        local playerAccount = getPlayerAccount(source) 
        local userAccountName = getAccountName (playerAccount) 
        dbExec(dataBase, "UPDATE userDatabase SET playerDeaths = IFNULL (playerDeaths, 0) + 1 WHERE accountName = ?", userAccountName) 
end 
 addEventHandler ("onPlayerWasted",root,deathPlayer) 
-- STATICS: JOINS -- 
function joinsX(_,uName) 
    local userAccountName = getAccountName (uName) 
    dbExec(dataBase, "UPDATE userDatabase SET playerJoin = IFNULL (playerJoin, 0) + 1 WHERE accountName = ?", userAccountName) 
end 
 addEventHandler ("onPlayerLogin",root,joinsX) 

Link to comment

Full server side:

local dataBase = dbConnect ( "sqlite", "userDatabase.db" ) 
dbExec (dataBase ,"CREATE TABLE IF NOT EXISTS userDatabase(accountName TEXT,money INT,playerKills INT,playerDeaths INT,playerJoin INT)" ) 
  
-- ADD ACCOUNT DATA -- 
addEventHandler ("onPlayerLogin",root, 
function(_,acName) 
    local userAccountName = getAccountName ( acName ) 
    local accountNameQuery = dbQuery ( dataBase, "SELECT accountName FROM userDatabase WHERE accountName = ?", userAccountName ) 
    local result = dbPoll ( accountNameQuery, -1 ) 
    if ( #result == 0 ) then 
        local createData = dbExec ( dataBase, "INSERT INTO userDatabase(accountName,money,playerKills,playerDeaths,playerJoin) VALUES (?,?,?,?,?)",userAccountName,0,0,0,0) 
        if ( createData ) then 
            outputChatBox ("#FF8600*#C1C1C1Your account data was successfully created.",source,255,255,255,true) 
        end 
    end 
end 
) 
  
-- DELETE ACCOUNT DATA -- 
addCommandHandler ("deleteAccountData", 
function(thePlayer,command,accName) 
    local aName = getAccountName( getPlayerAccount(thePlayer) ) 
    if isObjectInACLGroup("user."..aName, aclGetGroup("Admin")) then 
        if accName then 
            local accountNameDB = dbQuery(dataBase, "SELECT accountName FROM userDatabase WHERE accountName = ?", accName) 
            local result = dbPoll(accountNameDB, -1) 
            if #result ~= 0 then 
                deleteUserData = dbExec (dataBase,"DELETE FROM userDatabase WHERE  accountName=?", accName) 
                if (deleteUserData) then 
                    outputChatBox ("#FF8600*#C1C1C1Account data was deleted. ("..accName..")",source,255,255,255,true) 
                end 
            else 
                outputChatBox ("#FF8600*#C1C1C1Account name could not be found. ("..accName..")",source,255,255,255,true) 
            end 
        end 
    end 
end 
) 
-- ACCOUNT CLIENT-SIDE -- 
function gAccountsToSend( user ) 
    local gAccount = getPlayerAccount( user ) 
    local gAccountName = getAccountName(gAccount) 
    if (gAccountName) then 
        local query = dbQuery (dataBase,"SELECT money,playerKills,playerDeaths,playerJoin FROM userDatabase WHERE accountName = ?",gAccountName) 
        local result = dbPoll (query,-1) 
        if ( #result > 0 ) then 
            local userMoney = result.money 
            local userKills = result.playerKills 
            local userDeaths = result.playerDeaths 
            local userJoins = result.playerJoin 
            triggerClientEvent( client, "onSendAccountNamesToClient", client, userMoney,userKills,userDeaths,userJoins) 
        end 
    end 
end 
addEvent("onClientRequestAccountNames", true) 
addEventHandler("onClientRequestAccountNames", getRootElement(), gAccountsToSend) 
  
-- STATICS: DEATHS -- 
function deathPlayer() 
        local playerAccount = getPlayerAccount(source) 
        local userAccountName = getAccountName (playerAccount) 
        dbExec(dataBase, "UPDATE userDatabase SET playerDeaths = IFNULL (playerDeaths, 0) + 1 WHERE accountName = ?", userAccountName) 
end 
 addEventHandler ("onPlayerWasted",root,deathPlayer) 
-- STATICS: JOINS -- 
function joinsX(_,uName) 
    local userAccountName = getAccountName (uName) 
    dbExec(dataBase, "UPDATE userDatabase SET playerJoin = IFNULL (playerJoin, 0) + 1 WHERE accountName = ?", userAccountName) 
end 
 addEventHandler ("onPlayerLogin",root,joinsX) 

Client side:

function getGridPlayer(userMoney,userKills,userDeaths,userJoins) 
    if ( guiGridListGetSelectedItem ( playersGridList ) ) then 
        local thePlayerName = guiGridListGetSelectedItemText ( playersGridList ) 
        local getUserName = getPlayerFromPartialName (thePlayerName) 
        guiSetText (nameLabel,"Username: "..thePlayerName) 
        outputDebugString ( "UserMoney: ".. tostring ( userMoney ) ) 
        outputDebugString ( "UserKills: ".. tostring ( userKills ) ) 
        outputDebugString ( "UserDeaths: ".. tostring ( userDeaths ) ) 
        outputDebugString ( "UserJoins: ".. tostring ( userJoins ) ) 
        triggerServerEvent("onClientRequestAccountNames", getLocalPlayer(),getUserName) 
        guiSetText (moneyLabel,"Money: ".. tostring ( userMoney )  ) 
        guiSetText (killsLabel,"Kills: ".. tostring ( userKills )  ) 
        guiSetText (deathsLabel,"Deaths: ".. tostring ( userDeaths )  ) 
        guiSetText (joinLabel,"Joins: ".. tostring ( userJoins ).."x") 
    end 
end 
addEvent("onSendAccountNamesToClient", true) 
addEventHandler("onSendAccountNamesToClient", getRootElement(), getGridPlayer) 
addEventHandler ("onClientGUIClick",root,getGridPlayer) 

Link to comment

I think I solved your problem, try that and tell me the results

--Client side:

function getGridPlayer ( getUserName, userMoney, userKills, userDeaths, userJoins ) 
    guiSetText (nameLabel,"Username: ".. tostring ( getPlayerName ( getUserName ) ) )      
    guiSetText (moneyLabel,"Money: ".. tostring ( userMoney )  ) 
    guiSetText (killsLabel,"Kills: ".. tostring ( userKills )  ) 
    guiSetText (deathsLabel,"Deaths: ".. tostring ( userDeaths )  ) 
    guiSetText (joinLabel,"Joins: ".. tostring ( userJoins ).."x") 
end 
addEvent ( "onSendAccountNamesToClient", true ) 
addEventHandler("onSendAccountNamesToClient", root, getGridPlayer) 
  
function onGridClick( ) 
    if ( source == playersGridList ) then 
        local getUserName = getPlayerFromName ( guiGridListGetItemText ( playersGridList, guiGridListGetSelectedItem ( playersGridList ), 1) ) 
        if ( getUserName ) then  
            triggerServerEvent ( "onClientRequestAccountNames", localPlayer, getUserName ) 
        end  
    end      
end  
addEventHandler ( "onClientGUIClick", guiRoot, onGridClick ) 

--Server side:

Change the gAccountsToSendfunction with this one:

-- ACCOUNT CLIENT-SIDE -- 
function gAccountsToSend( getUserName ) 
    local gAccount = getPlayerAccount( getUserName ) 
    local gAccountName = getAccountName ( gAccount ) 
    if ( gAccountName ) then 
        local query = dbQuery (dataBase,"SELECT money,playerKills,playerDeaths,playerJoin FROM userDatabase WHERE accountName = ?",gAccountName) 
        local result = dbPoll (query,-1) 
        if ( #result > 0 ) then 
            local userMoney = result.money 
            local userKills = result.playerKills 
            local userDeaths = result.playerDeaths 
            local userJoins = result.playerJoin 
            triggerClientEvent( client, "onSendAccountNamesToClient", client, getUserName, userMoney, userKills, userDeaths, userJoins) 
        end 
    end 
end 
addEvent("onClientRequestAccountNames", true) 
addEventHandler("onClientRequestAccountNames", getRootElement(), gAccountsToSend) 

Link to comment

The code that I wrote should work, debug the outputted values from database

-- ACCOUNT CLIENT-SIDE -- 
function gAccountsToSend( getUserName ) 
    local gAccount = getPlayerAccount( getUserName ) 
    local gAccountName = getAccountName ( gAccount ) 
    if ( gAccountName ) then 
        local query = dbQuery (dataBase,"SELECT money,playerKills,playerDeaths,playerJoin FROM userDatabase WHERE accountName = ?",gAccountName) 
        local result = dbPoll (query,-1) 
        if ( #result > 0 ) then 
            local userMoney = result.money 
            local userKills = result.playerKills 
            local userDeaths = result.playerDeaths 
            local userJoins = result.playerJoin 
            outputDebugString ( tostring ( userMoney ) ) 
            outputDebugString ( tostring ( userKills ) )  
            outputDebugString ( tostring ( userDeaths ) ) 
            outputDebugString ( tostring ( userJoins ) )  
            outputDebugString ( tostring ( getPlayerName ( getUserName ) ) 
            triggerClientEvent( client, "onSendAccountNamesToClient", client, getUserName, userMoney, userKills, userDeaths, userJoins) 
        end 
    end 
end 
addEvent("onClientRequestAccountNames", true) 
addEventHandler("onClientRequestAccountNames", getRootElement(), gAccountsToSend) 

Link to comment

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...