JSON returns null when trying to get information from the database via PHP

Asked

Viewed 30 times

1

I am trying to get information from php database and pass to JSON, however it always comes empty returns null and without any error.

[
    {
        "current_field": null,
        "field_count": null,
        "lengths": null,
        "num_rows": null,
        "type": null
    }
]

Below the PHP code:

<?php 

require 'conexao.php';

$temperatura = "SELECT * FROM estufas";

$resultado_temperatura = mysqli_query($conexao,$temperatura)or 
die(mysqli_error($conexao));

if (mysqli_num_rows($resultado_temperatura) > 0) {

    $dados[] = $resultado_temperatura;

    echo json_encode($dados, JSON_PRETTY_PRINT);
}

?>

1 answer

1


I managed to solve, the code was like this:

<?php 

require 'conexao.php';

$array = array();

$resultado = "SELECT Temperatura_Estufa,Umidade_Estufa FROM estufas";

$res = mysqli_query($conexao,$resultado)or die(mysqli_error($conexao));

while ($rows = mysqli_fetch_assoc($res)) {
$dados[] = array('Temperatura_Estufa' => $rows['Temperatura_Estufa'],
                 'Umidade_Estufa' => $rows['Umidade_Estufa'],);
}    
echo json_encode($dados);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.