J'essaie d'obtenir un tableau de vérification à remplir avant le tableau mysql dans json_encode. c'est le tableau que je voudrais avant le tableau mysql "array (" status ":" true "," message ":" Données récupérées avec succès! "," data ":" Mais quand j'exécute le service Web, il est juste vide . Des idées?
{"status":"true","message":"Data fetched successfully!","data":[{"id":"1","name":"Roger Federer","country":"Switzerland","city":"Basel","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/roger.jpg"},{"id":"2","name":"Rafael Nadal","country":"Spain","city":"Madrid","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/nadal.jpg"},{"id":"3","name":"Novak Djokovic","country":"Serbia","city":"Monaco","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/djoko.jpg"},{"id":"4","name":"Andy Murray","country":"United Kingdom","city":"London","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/murray.jpg"},{"id":"5","name":"Maria Sharapova","country":"Russia","city":"Moscow","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/shara.jpg"},{"id":"6","name":"Caroline Wozniacki","country":"Denmark","city":"Odense","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/woz.jpg"},{"id":"7","name":"Eugenie Bouchard","country":"Canada","city":" Montreal","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/bou.png"},{"id":"8","name":"Ana Ivanovic","country":"Serbia","city":"Belgrade","imgURL":"https:\/\/demonuts.com\/Demonuts\/SampleImages\/iva.jpg"}]}
Voici à quoi j'aimerais qu'elle ressemble:
<?php
// Create connection
$con=mysqli_connect("localhost","burtkunt_dbuser","phatelives","burtkunt_colors");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM colors";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode(array("status":"true","message":"Data fetched successfully!","data":$resultArray));
}
// Close connections
mysqli_close($con);
?>
3 Réponses :
votre tableau est dans un format incorrect car
c'est le format correct
$array = array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
$json_arr = array("status"=>"true","message"=>"Data fetched successfully!","data"=> $resultArray);
echo json_encode($json_arr);
Vous devez d'abord le sélectionner pour valoriser votre base de données à l'intérieur tout comme ceci
C'est ma façon de créer json lorsque vous sélectionnez des données de DB
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
$data = array("id" => $row['id'], "name" => $row['name'],"country" => $row['country']);
array_push($temparray, $data);
}
$arr= array("status"=>"true","message"=>"Data fetched successfully!", "data" => $temparray);
echo json_encode($arr);
J'espère que cela vous aidera p>
L'affectation de votre tableau PHP est incorrecte. En outre, j'ai trouvé un autre problème avec votre code. Voici une version améliorée
<?php
// Create connection
$con=mysqli_connect("localhost","burtkunt_dbuser","phatelives","burtkunt_colors");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM colors";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
// $tempArray = array(); // unnecessary
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
// $tempArray = $row;
array_push($resultArray, $row);
}
// Finally, encode the array to JSON and output the results
echo json_encode(array("status" => "true","message" => "Data fetched successfully!","data" => $resultArray));
}
// Close connections
mysqli_close($con);
?>
Vous avez une syntaxe PHP invalide.
(array ("status": "true", "message": "Données récupérées avec succès!", "data": $ resultArray) ce n'est pas un tableau