Selecting from a database record using variables for field references

Is this possible in Lua?

In other languages, I’m used to fetching a record as an array/table and being able to just grab the appropriate field from that set using variables. Something like this:

$field1 = "fldSomething"; $field2 = "fldSomethingElse"; $record = dbFetch(" SELECT \* FROM `tblTable` WHERE fldFoo = 'Bah' "); echo $field1 . " = " . $record[$field1]; echo $field2 . " = " . $record[$field2];

But in Lua, records don’t seem to be returned as assoc tables and I’m struggling to find a workaround?

I.e. say I’m iterating records like this:

for result in db:nrows( [[SELECT \* FROM `tblTable` WHERE fldFoo = "Bah";]] ) do print("fldSomething = " .. result.fldSomething ) end

How would I amend that print statement if fldSomething isn’t a hard-coded name?

local field1 = "fldSomething" for result in db:nrows( [[SELECT \* FROM `tblTable` WHERE fldFoo = "Bah";]] ) do -- I obviously can't just do result.field1 here, because field1 isn't a property of result... print(field1 .. " = " .. result.field1 ) end

Cheers.

Scrap this, I’m being thick. Turns out result can be read as a table too, so result[field1] works just fine.

Clearly it’s past my bedtime!

Richard,

As busy as and productive as you are, I’m surprised to learn that you have a bedtime.  :wink:

Ha. Mathematically I’m 33, but both physically and mentally I’m well over 100. It’s difficult to stay awake after 9pm when you’re over 100…

Scrap this, I’m being thick. Turns out result can be read as a table too, so result[field1] works just fine.

Clearly it’s past my bedtime!

Richard,

As busy as and productive as you are, I’m surprised to learn that you have a bedtime.  :wink:

Ha. Mathematically I’m 33, but both physically and mentally I’m well over 100. It’s difficult to stay awake after 9pm when you’re over 100…