Jump to content

help with mysql / JSON


lgeri000

Recommended Posts

Posted

Hi guys.

Im trying to make an inventory system, but i cant save the table that contains the items. So the code is:

local inv = {
	[1] = {{1,false,false,100},{2,false,false,100},{3,false,false,90},{4,false,false,100},{5,false,false,95},{6,false,1000,99},{7,false,1001,100},{8,false,1002,100},{9,false,1003,100},{10,false,1004,30},{11,false,1005,45},{12,false,1006,55},{13,false,1007,60},{14,false,1008,65},{15,false,1009,70},{16,false,1010,75},{17,false,1011,80},{18,false,1012,90},{19,false,1013,99},{13,false,1014,100},{15,false,1015,100}},
	[2] = {{1,20,false,false},{2,30,false,false},{3,30,false,false},{4,110,false,false},{5,100,false,false},{6,2,false,false},{7,1,false,false},{8,1,false,false},{9,1,false,false}},
	[3] = {},
	[4] = {},
	[5] = {},
	[6] = {},
	[7] = {},
	[8] = {},
}

dbExec(con,"update characters set inventory=? where id=?",toJSON(inv),1)
local qh = dbQuery(con,"select * from characters where id=?",1)
local invent = dbPoll(qh,100)
invent = fromJSON(invent[1]["inventory"])
outputChatBox(#invent)
setElementData(getElementsByType("player")[1],"inventory",invent)

and the invent variable is not a table. If i save only the first 2 table in the "inv" table then it works so its probably something with the empty tables.

Probably i dont know something about mysql or JSON, i would be very glad if someone could help me with this.

Thanks :)

  • Moderators
Posted
2 hours ago, lgeri000 said:

Probably i dont know something about mysql or JSON

Please show the JSON, that is the easiest way of checking why the conversion is failing.

 

Also don't use the following array formatting:

{
[1] = {},
[2] = {}
}

Keep it clean, no gaps if possible, if you do have gaps add a false value or an empty table:

{
	{},
	{}
}

The conversion between Lua and JSON is rather sensitive if you do not follow the JavaScript Object Notion rules.


Another golden rule:
Don't mix keys of different key types, it is either strings or integer each (sub)table. If one of them is a string, all of them become strings. There is no such thing as a table in JavaScript, there is only an default object(key strings) or an array(key integers).

 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   ?

 

  Useful functions  3x 

  Tutorials  4x 

 

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