I’m trying to get som data from a database via php scripts and into my app. I’ve done this successfully before, but now json.decode() acts up on me and always return nil.
This is the app code:
local json = require("json") local myNewData local decodedData local function checkItemNetworkListener( event ) if ( event.isError ) then print( "Network error!") else myNewData = event.response print ("From server: '" .. myNewData .. "'") decodedData = (json.decode(myNewData)) print ("Decoded data: " .. decodedData) local numRows = #decodedData print("Number of rows returned from database: " .. numRows) end end local php = "http://allthumbsdev.com/RegIt/test.php?it\_id=5728JHGK-NO2" print("PHP: " .. php) network.request(php, "GET", checkItemNetworkListener )
When I run this code, this is the output:
PHP: http://allthumbsdev.com/RegIt/item_request.php?it_id=5728JHGK-NO2
From server: ’
[{“Brand”:“Sony”,“Category”:“0”,“SubCategory”:“0”,“Status”:“0”}]’
Runtime error
d:\programming\programming\corona\test cases\php\main.lua:20: attempt to concatenate upvalue ‘decodedData’ (a table value)…
The data looks good, but there seems to be something in front that shouldn’t be there. Maybe that
The simplified (removed proper error handling) PHP-script looks like this:
\<?php include 'utils.php'; $itemId = $\_GET["it\_id"]; $conn = mysqli\_connect(xxxxxxxxxxxxxxxxxxxxxxx); $retValJSON = array(); $sql = "SELECT Brand, Category, SubCategory, Status FROM regit\_item WHERE Id='$itemId' $BRAND\_S ORDER BY Category, SubCategory"; $result = mysqli\_query($conn, $sql); if ($result) { if ($result-\>num\_rows \> 0) while ($row = $result-\>fetch\_assoc()) $retValJSON[]=$row; } mysqli\_close($conn); echo json\_encode($retValJSON); ?\>
I have three questions:
-
Where are those extra characters coming from?
-
Are they the reason json.decode() returns nil?
-
How do I get rid of them
Thanks!