Jump to content

Can someone please explain me this code?


TopAz

Recommended Posts

Please explain more better.

Well this is what I want. As you can see I have a table named pldata which consist few cells. Inside a cell there are two parameters. One is the "data name" and the other is the "data value". Now what I want is that I want to assign those data values on the data values which has been queried from mysql database and which is on the poll table. How would I be able to do that?

Link to comment

You mean this?

Try

local connectionHandle; 
  
local pldata = 
{ 
    { "id", "0" }; 
    { "name", "N/A" }; 
    { "age", "0" }; 
    { "profession", "N/A" }; 
    { "language", "N/A" }; 
} 
  
addEventHandler( 'onPlayerJoin', root, 
    function( ) 
        spawnPlayer( source, 1959.55, -1714.46, 10 ) 
        fadeCamera( source, true ) 
        setCameraTarget( source, source ) 
    end 
) 
  
addEventHandler( 'onResourceStart', resourceRoot, 
    function( ) 
        connectionHandle = dbConnect( "mysql", "dbname=testgm;host=localhost", "root", "" ) 
        local tQuery = dbQuery( connectionHandle, "SELECT * FROM `user` WHERE `id` = 1" ) 
        local tResult, nNumrows, sErrmsg = dbPoll ( tQuery , -1 ) 
        if nNumrows > 0 then 
            for nKey, value in ipairs( tResult ) do 
                pldata[ nKey ][ 2 ] = value 
            end 
        end 
    end 
) 
  

or this

local connectionHandle; 
  
local pldata = 
{ 
    { "id", "0" }; 
    { "name", "N/A" }; 
    { "age", "0" }; 
    { "profession", "N/A" }; 
    { "language", "N/A" }; 
} 
  
addEventHandler( 'onPlayerJoin', root, 
    function( ) 
        spawnPlayer( source, 1959.55, -1714.46, 10 ) 
        fadeCamera( source, true ) 
        setCameraTarget( source, source ) 
    end 
) 
  
addEventHandler( 'onResourceStart', resourceRoot, 
    function( ) 
        connectionHandle = dbConnect( "mysql", "dbname=testgm;host=localhost", "root", "" ) 
        local tQuery = dbQuery( connectionHandle, "SELECT * FROM `user` WHERE `id` = 1" ) 
        local tResult, nNumrows, sErrmsg = dbPoll ( tQuery , -1 ) 
        if nNumrows > 0 then 
            for nKey, value in ipairs( tResult[1] ) do 
                pldata[ nKey ][ 2 ] = value 
            end 
        end 
    end 
) 
  

I not know exactly.

I not use mta mysql functions.

I use mysql module :mrgreen:

Updated.

Edited by Guest
Link to comment
local connectionHandle; 
  
local pldata = { } 
  
addEventHandler ( "onPlayerJoin", root, 
    function ( ) 
        spawnPlayer ( source, 1959.55, -1714.46, 10 ) 
        fadeCamera ( source, true ) 
        setCameraTarget ( source, source ) 
    end 
) 
  
addEventHandler ( "onResourceStart", root, 
    function ( ) 
        connectionHandle = dbConnect ( "mysql", "dbname=testgm;host=localhost", "root", "" ) 
        local query = dbQuery ( connectionHandle, "SELECT * FROM `user`" ) 
        if ( query ) then 
            for key, value in ipairs ( dbPoll ( query, -1 ) ) do 
                table.insert ( pldata, { id = value[ "id" ], name = value[ "name" ], age = value[ "age" ], profession = value[ "profession" ], language = value[ "language" ] } ) 
            end 
        end 
    end 
) 

Link to comment

I have improved the following code and it should work properly.

  
local root = getRootElement() 
local connectionHandle; 
  
local pldata =  -- Initializing the pldata table 
{ 
    -- Instead of putting them on a singluar cell, assign it in each id following your data name in encased squared brackets and assigning it to the default value. 
    ["id"] = "0",  
    ["name"] = "N/A", 
    ["age"] = "0", 
    ["profession"] = "N/A", 
    ["language"] = "N/A" 
} 
  
addEventHandler("onPlayerJoin", root,  
    function() 
        spawnPlayer(source, 1959.55, -1714.46, 10) 
        fadeCamera(source, true) 
        setCameraTarget(source, source) 
    end 
) 
  
addEventHandler("onResourceStart", root,  
    function() 
        connectionHandle = dbConnect("mysql", "dbname=testgm;host=localhost", "root", "") 
        local query = dbQuery(connectionHandle, "SELECT * FROM `user` WHERE `id` = 1") 
        local result, numrows, errormsg = dbPoll(query, -1) 
        dbFree(query) 
        if numrows > 0 then 
            -- Now this is what we are doing. For each values on the keys of result table execute the following code. 
            for k, v in pairs(result) do   
                for a, b in pairs(v) do  -- And this for each data name and data value for e.j. name | N/A 
                    pldata[a] = b -- Then setting it up. It can be pldata["name"] = "Another name" 
                end 
            end 
        end 
    end 
) 
  

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