json.decode not working

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

I am also having this very same problem and can’t figure it out. 

Have you figured it out yet? or can someone else help?

Thanks

Please try to minimize your code before posting, presumably all the sqlite code is irrelevant to the problem. You are much more likely to get a prompt response then.

If you put http://localhost/regie/sqlLite.php in your browser, do you see the JSON?

Does line 51 print nil?

well I can’t speak for him, but my code is as follows 

local sqlite3 = require ("sqlite3") local myNewData local json = require ("json") local decodedData local SaveData = function () --save new data to a sqlite file -- open SQLite database, if it doesn't exist, create database local path = system.pathForFile("movies.sqlite", system.DocumentsDirectory) db = sqlite3.open( path ) print(path) -- setup the table if it doesn't exist local tablesetup = "CREATE TABLE IF NOT EXISTS mymovies (id INTEGER PRIMARY KEY, movie, year);" db:exec( tablesetup ) print(tablesetup) -- save data to database local counter = 1 local index = "movie"..counter local movie = decodedData[index] print(movie) while (movie ~=nil) do local tablefill ="INSERT INTO mymovies VALUES (NULL,'" .. movie[2] .. "','" .. movie[3] .."');" print(tablefill) db:exec( tablefill ) counter=counter+1 index = "movie"..counter movie = decodedData[index] end -- Everything is saved to SQLite database; close database db:close() --Load database contents to screen -- open database local path = system.pathForFile("movies.sqlite", system.DocumentsDirectory) db = sqlite3.open( path ) print(path) --print all the table contents local sql = "SELECT \* FROM mymovies" for row in db:nrows(sql) do local text = row.movie.." "..row.year local t = display.newText(text, 20, 30 \* row.id, native.systemFont, 24) t:setTextColor(255,255,255) end db:close() end local function networkListener( event ) if ( event.isError ) then print( "Network error!") else myNewData = event.response print ("From server: "..myNewData) decodedData = (json.decode( myNewData)) SaveData() end end network.request( "http://www.localhost.com/json.php", "GET", networkListener )

and it prints “From server:” but thats it, it doesn’t include the event.response

What about if you put http://www.localhost.com/json.php in your browser. Do you see the json?

If there is nothing there then you have something going in with the server.

I am also having this very same problem and can’t figure it out. 

Have you figured it out yet? or can someone else help?

Thanks

Please try to minimize your code before posting, presumably all the sqlite code is irrelevant to the problem. You are much more likely to get a prompt response then.

If you put http://localhost/regie/sqlLite.php in your browser, do you see the JSON?

Does line 51 print nil?

well I can’t speak for him, but my code is as follows 

local sqlite3 = require ("sqlite3") local myNewData local json = require ("json") local decodedData local SaveData = function () --save new data to a sqlite file -- open SQLite database, if it doesn't exist, create database local path = system.pathForFile("movies.sqlite", system.DocumentsDirectory) db = sqlite3.open( path ) print(path) -- setup the table if it doesn't exist local tablesetup = "CREATE TABLE IF NOT EXISTS mymovies (id INTEGER PRIMARY KEY, movie, year);" db:exec( tablesetup ) print(tablesetup) -- save data to database local counter = 1 local index = "movie"..counter local movie = decodedData[index] print(movie) while (movie ~=nil) do local tablefill ="INSERT INTO mymovies VALUES (NULL,'" .. movie[2] .. "','" .. movie[3] .."');" print(tablefill) db:exec( tablefill ) counter=counter+1 index = "movie"..counter movie = decodedData[index] end -- Everything is saved to SQLite database; close database db:close() --Load database contents to screen -- open database local path = system.pathForFile("movies.sqlite", system.DocumentsDirectory) db = sqlite3.open( path ) print(path) --print all the table contents local sql = "SELECT \* FROM mymovies" for row in db:nrows(sql) do local text = row.movie.." "..row.year local t = display.newText(text, 20, 30 \* row.id, native.systemFont, 24) t:setTextColor(255,255,255) end db:close() end local function networkListener( event ) if ( event.isError ) then print( "Network error!") else myNewData = event.response print ("From server: "..myNewData) decodedData = (json.decode( myNewData)) SaveData() end end network.request( "http://www.localhost.com/json.php", "GET", networkListener )

and it prints “From server:” but thats it, it doesn’t include the event.response

What about if you put http://www.localhost.com/json.php in your browser. Do you see the json?

If there is nothing there then you have something going in with the server.