I developed an android application with Mysql database. I would like the result of my sql query is displayed in a JSON variable in a table. If I use a single column of my TableA "country" ---> "id_country" or "name_en_country" it works but if I want to display more columns ---> "id_country" AND "name_en_country" AND more .. ... The result Requet php send me a blank page. Could you help me please thank you!
<?php
// Create Database connection
$mysqli = new mysqli("localhost", "root", "", "whenmeeat");
if (!$mysqli) {
printf("Échec de la connexion : %s
", mysqli_connect_error());
}
// Replace * in the query with the column names.
$result = $mysqli->query("select id_country, name_en_country, name_fr_country from country", MYSQLI_USE_RESULT);
// Create an array
$json_response["country"] = array();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$row_array['id_country'] = $row['id_country'];
$row_array['name_en_country'] = $row['name_en_country'];
// push the values in the array
array_push($json_response["country"],$row_array);
}
echo json_encode($json_response["country"]);
// Close the database connection
$mysqli->close();
?>
Based on the example set here json_encode() array in while loop for mySQL for calendar your code should alter like this:
<?php
// Create Database connection
$mysqli = new mysqli("localhost", "root", "", "whenmeeat");
if (!$mysqli) {
printf("Échec de la connexion : %s
", mysqli_connect_error());
}
// Replace * in the query with the column names.
$stmt = $mysqli->prepare("SELECT `id_country`, `name_en_country`, `name_fr_country` FROM `country`");
$stmt->execute();
$res = $stmt->get_result();
// Create an array
$json = array();
while ($row = $res->fetch_assoc()) {
$country = array(
'id_country' => $row['id_country'],
'name_en_country' => $row['name_en_country'],
'name_fr_country' => $row['name_fr_country']
);
$json[] = $country ;
}
echo json_encode($json);
// Close the database connection
$mysqli->close();
?>