Hello I am trying to use php to access my data base from a Swift app I am coding. So far reading the tables has been going great, except now I am trying to read a table that has multiple rows containing json. This has been throwing errors and I can not seem to get the final output to equal what I want, or anything that works with the swift code for that matter. The json originally just outputted as null. Upon researching how to fix that I tried utf8_encode() but that gave too many extra characters and the Swift code in the app couldn't make sense of it. When outputting just one of the rows it comes out fine, Its when I try putting them in one associative array to out put as json is when they come up as null.
PHP Code:
$sql = "Select * FROM User WHERE Id = '".$UserId."' LIMIT 1";
mysql_select_db($database, $User);
$result = mysql_query($sql , $User) or die(mysql_error());
$FleetRaw = mysql_fetch_assoc($result);
$Fleet1 = $FleetRaw['Fleet1'];
$Fleet2 = $FleetRaw['Fleet2'];
$Fleet3 = $FleetRaw['Fleet3'];
$Fleet4 = $FleetRaw['Fleet4'];
$Fleet5 = $FleetRaw['Fleet5'];
$Fleet6 = $FleetRaw['Fleet6'];
$Fleets = array("1"=>$Fleet1,"2"=>$Fleet2,"3"=>$Fleet3,"4"=>$Fleet4,"5"=>$Fleet5,"6"=>$Fleet6);
//Output 1
echo $Fleets["1"]."<br><br><br>";
//Output 2
echo json_encode(utf8_encode($Fleets["1"]))."<br><br><br>";
//Output 3
echo json_encode($Fleets);
?>
Outputs:
Output 1:
{ “status” : 3, “game” : 0, “ships” : { "1" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 100 }, "3" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : -100 }, "2" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 0 }, "0" : { "level" : 0, "className" : "MotherShip", "posX" : 0, "health" : 100, "posY" : 0 } } }
Output 2:
"{\n\u0093status\u0094 : 3,\n\u0093game\u0094 : 0,\n\u0093ships\u0094 : {\n \"1\" : {\n \"level\" : 0,\n \"className\" : \"LighteningShip\",\n \"posX\" : 100,\n \"health\" : 50,\n \"posY\" : 100\n },\n \"3\" : {\n \"level\" : 0,\n \"className\" : \"LighteningShip\",\n \"posX\" : 100,\n \"health\" : 50,\n \"posY\" : -100\n },\n \"2\" : {\n \"level\" : 0,\n \"className\" : \"LighteningShip\",\n \"posX\" : 100,\n \"health\" : 50,\n \"posY\" : 0\n },\n \"0\" : {\n \"level\" : 0,\n \"className\" : \"MotherShip\",\n \"posX\" : 0,\n \"health\" : 100,\n \"posY\" : 0\n }\n}\n}"
Output 3:
{"1":null,"2":null,"3":null,"4":null,"5":null,"6":null}
Output 1 is exactly the format I want (the one Swift understands), except it is only one of the six rows (Also app rejects this form because it is not json_encode before echoing). Output 2 is an example of one of the six rows that when used utf8_encode() before saved to the array gives to many extra characters, however it does output as not null when put into an array of the six. Output 3 is what I want to eventually output, just without the null.
The ideal situation would be to combine outputs 1 and 3 so that I can output an array of six with them looking like Output 1. Also the app has only worked when I json_encode what I echo. If there is anyone possible to accomplish this please let me know!!
Thanks!!
closest attempt, working but double the data?
$Fleet1 = $FleetRaw['Fleet1'];
$Fleet2 = $FleetRaw['Fleet2'];
$Fleet3 = $FleetRaw['Fleet3'];
$Fleet4 = $FleetRaw['Fleet4'];
$Fleet5 = $FleetRaw['Fleet5'];
$Fleet6 = $FleetRaw['Fleet6'];
$Fleets = array("1"=>$Fleet1,"2"=>$Fleet2,"3"=>$Fleet3,"4"=>$Fleet4,"5"=>$Fleet5,"6"=>$Fleet6);
// Convert an array of JSON-Strings to unified array of structured data..
foreach ( $Fleets as $key => $sJSONString ){
$FleetRaw[$key] = json_decode($sJSONString);
}
// Now return the whole lot as a json-string to the client
header("Content-type: application/json"); // My assumption of your model..
print json_encode($Fleets);