Hi there guys!
I am facing a problem with my database connection.
So i have this php code:
$hostname = “localhost”;
$database = “regie”;
$connection = mysql_connect("$hostname","$username","$password") or die (“Connection could not be established”);
mysql_select_db(“regie”) or die (“Cannot access database”);
$query = “SELECT * FROM localuri”;
$result = mysql_query($query);
$json = array();
$count = 0;
if(mysql_num_rows($result)){
while($row = mysql_fetch_row($result)){
$count = $count+1;
$json[“local”.$count] = $row;
}
}
mysql_close($regie);
echo(json_encode($json));
?>
And i have this lua code:
[lua]
local sqlLite3 = require “sqlite3”
local json = require (“json”)
local myNewData
local decodedData
local saveData = function()
local path = system.pathForFile( “localuridb.sqlite” , system.DocumentsDirectory )
db = sqlLite3.open(path)
print(path)
local tableSetup = “CREATE TABLE IF NOT EXISTS localuri(id integer primary key, nume, meniu_mancare, meniu_bautura, evenimente, contact)”
db:exec(tableSetup)
print(tableSetup)
local counter = 1
local index = “local”…counter
local localul = decodedData[index]
print(localul)
while (localul~=nil) do
local tableFill = “INSERT INTO localuri VALUES (null,’”…localul[2]…"’,’"…localul[3]…"’,’"…localul[4]…"’,’"…localul[5]…"’,’"…localul[6]…"’);"
print (tableFill)
db:exec(tableFill)
counter = counter+1
index = “local”…counter
localul = decodedData[index]
end
db:close()
local path = system.pathForFile( “localuridb.sqlite” , system.DocumentsDirectory )
db = sqlLite3.open(path)
print(path)
local query = “SELECT * FROM localuri;”
print(query)
db:exec(query)
for row in db:rows(query) do
print(row.nume)
end
db:close()
end
local function networkListener(event)
if event.isError then
print( “Network Listener error” )
else
myNewData = event.response
print("From server: "… myNewData)
decodedData = json.decode(event.response)
saveData()
end
end
network.request( “http://localhost/regie/sqlLite.php”, “GET”, networkListener );
[/lua]
The print("From server: "… myNewData) statement from the networkListener works well, but it seems that json.decode(event.response) returns a nil value. I get “attempt to index up value decodedData (a nil value)”
Please help me with this, I spent a lot of time trying to figure out the problem, but with no success