PHP Code:
function mysql_query(query,user,pw,db,ip) -- Gibt ALLE Werte als STR zurück.
local var = {}
var.pre = ''
if ip ~= nil then
var.pre = var.pre..' -h'..ip
end
if user ~= nil then
var.pre = var.pre..' -u'..user
end
if pw ~= nil then
var.pre = var.pre..' -p'..pw
end
if db ~= nil then
var.pre = var.pre..' -D'..db
end
var.scriptfile = 'sc_'..pc.get_name()
var.outputfile = 'op_'..pc.get_name()
query= string.gsub(query,'"',"'")
var.str = "mysql "..var.pre.." < "..var.scriptfile.." > "..var.outputfile
script = io.open(var.scriptfile,"w")
script:write(query)
script:close()
os.execute(var.str)
local g = {}
local f = io.open(var.outputfile)
g.i = 0
g.li={}
g.out= {}
g.count = 0
for line in f:lines() do
g.i = g.i+1
g.li[g.i] = line
end
-- TESTDEBUG
if g.li[1] == nil then
return "ERROR"
end
if string.len(g.li[1]) == 0 then
return "ERROR"
end
-- TESTDEBUG ENDE
-- Abfrage später : if out == "ERROR" then return end << Beispiel!!
g.fields = split(g.li[1],' ')
table.foreachi(g.fields,
function(lb,ln)
g.out[ln] = {}
end)
table.foreachi(g.li,
function(ou1,ou2)
if ou1 > 1 then
local la = split(ou2,' ')
g.count = g.count+1
table.foreachi(g.fields,
function(lb,ln)
g.out[ln][(ou1-1)] = la[lb]
end)
end
end
)
f:close()
-- Alle Strings, die nur Zahlen beinhalten, in Zahlen umwandeln
-- Zeigt gleichzeitig die Nutzungsmöglichkeit vom von mir modifizierten tonumber()
table.foreach(g.out,
function(i,l)
table.foreach(l,
function(i2,l2)
local d,e = tonumber(l2)
if e then
g.out[i][i2] = d
end
end
)
end
)
g.out.__data = {} -- Zur Ausgabe Queryspezifischer Daten
g.out.__data.fields = g.fields
g.out.__data.lines = g.count
g.out.__data.user = user
g.out.__data.pass = pw
g.out.__data.host = ip
g.out.__data.db = db
g.out.__data.query = query
os.execute("rm "..var.scriptfile)
os.execute("rm "..var.outputfile)
return g.out
end