简体   繁体   中英

PHP JSON Array empty even if the result query is not empty

so i have this php script here.

<?php
require "conn.php";

$response = array();

$query = "SELECT * FROM groupe_académiques";
$result = $conn->query($query);
$number_of_rows = mysqli_num_rows($result);


while ($row = mysqli_fetch_array($result)) {

    array_push($response, array("ID"=>$row[0], "Nom"=>$row[1]));

}

echo json_encode($response);


?>

And the thing is when i try it it's giving me an empty json array, even if the query is returning some rows. I used the same script for another table and it's working i really don't understand it's like there's no error, how can i solve that? thanks for your help.

Print data using print_r because your data not resturns in json format

<?php
require "conn.php";
$response = array();
$query = "SELECT * FROM groupe_académiques";
$result = $conn->query($query);
$number_of_rows = mysqli_num_rows($result);

while ($row = mysqli_fetch_array($result)) {
    array_push($response, array("ID"=>$row[0], "Nom"=>$row[1]));
}
echo "<pre>";print_r($response);echo "</pre>";
?>

Otherwise your code is correct.

Try Below Code       


    <?php

$sql="SELECT * FROM groupe_académiques";

$result = $conn->query($sql);

$myData=array(); // blank array
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
       array_push($myData,$row); // push rows to array $myData
    }
} 

echo json_encode($myData); //get json

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM