TopAz Posted April 2, 2012 Author Share Posted April 2, 2012 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
Kenix Posted April 2, 2012 Share Posted April 2, 2012 (edited) 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 Updated. Edited April 2, 2012 by Guest Link to comment
Castillo Posted April 2, 2012 Share Posted April 2, 2012 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
Cassandra Posted April 3, 2012 Share Posted April 3, 2012 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
Castillo Posted April 3, 2012 Share Posted April 3, 2012 @Cassandra: You should have read the whole topic, he firstly did that, but he wanted to do it indexed, e.g: id, name, age, etc, etc, with that it'll be like: name, id, profession. Link to comment
Cassandra Posted April 3, 2012 Share Posted April 3, 2012 I saw it. I gave him the most efficient way. It's upto him to decide further. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now