I saw many threads on stackoverflow where people converted arrays into json. And I am pretty sure that this should work, but it does not. MySQL is fine and the query is working as intended:
$json = array();
if($result = $mysqli->query($query)){
while($row = $result->fetch_array()){
$json[]["id"] = $row[0];
$json[]["trivialname"] = $row[1];
$json[]["chemischername"] = $row[2];
$json[]["formel"] = $row[3];
}
echo $json[0]["id"]; //just for testing.
echo json_encode($json);
}
Sorry for bad English.
My problem is mainly that echo json_encode($json); doesnt show anything
EDIT: My Code look like this atm.
if($result = $mysqli->query($query)){
$json = array();
while($row = $result->fetch_assoc()){
$json[]=$row;
printf("%s (%s) ID: %s, Formel:%s <br>", $row["Trvialname"], $row["Chemischername"], $row["ID"],$row["Formel"]);
}
echo json_encode($json);
}
and it shows me this:
Phthalimid (1,2-Benzoldicarboximid) ID: 2, Formel:C8H5NO2
Phthalsäureanhydrid (2-Benzofuran-1,3-dion) ID: 3, Formel:C8H4O3
nothing else! i can change:
echo json_encode($json);
to
echo json_encode($json[0]);
and it will add the line :
{"ID":"2","Trvialname":"Phthalimid","Chemischername":"1,2-Benzoldicarboximid","Formel":"C8H5NO2"}
foreach($json[1] as $value){
echo $value."<br>";
}
will show everything but
echo json_encode($json[1]);
doesnt.
Solution
$mysqli->set_charset('utf8');
thanks to Your Common Sense
[]var_dump($json)at the end?