Return JSON does not return in PHP

Asked

Viewed 115 times

1

It was with an API returning the data correctly, but it stopped out of nowhere.

The SQL statement when executed in Wrockbrench works. Other Apis follow this same pattern as below. But it is not returning. And I was returning before, I do not know what I changed that shows nothing of the result. Follow the codes:

My DAO:

<?php

class MensagemDAO {

    private $conexao;

    public function __construct() {
        $this->conexao = new Conexao();
    }

    public function consultarMensagens() {
        $sql = "SELECT * FROM mensagens WHERE enviado = 1";

        $resultado = mysqli_query($this->conexao->getCon(), $sql);

        if (mysqli_num_rows($resultado) > 0) {
            return $resultado;      
        } else {
            return false;            
        }
    }

    public function consultarCodigo($codigo) {
        $sql = "SELECT * FROM mensagens WHERE idmensagens = '$codigo'";

        $resultado = mysqli_query($this->conexao->getCon(), $sql);

        if (mysqli_num_rows($resultado) > 0) {
            return $resultado;
        } else {
            return false;
        }
    }

    public function consultarPremioMes() {
        $sql = "SELECT * FROM premio_mes WHERE NOW() >= data_inicio AND NOW() <= data_fim";

        $resultado = mysqli_query($this->conexao->getCon(), $sql);

        if (mysqli_num_rows($resultado) > 0) {
            return $resultado;
        } else {
            return false;
        }
    }

}
?>

My JSON Return API:

<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
//    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
        header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    }

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    }

    exit(0);
}

header('Content-type: application/json'); 


include "./../Classes/Conexao.php";
include './../Classes/DAO/MensagemDAO.php';


$MensagemDAO = new MensagemDAO();

$consulta = $MensagemDAO->consultarMensagens();

if ($consulta == true) {    
    for ($i = 0; $i < mysqli_num_rows($consulta); $i++) {
        $linha = mysqli_fetch_array($consulta);

        $respostas [] = array(
            'idmensagens' => $linha['idmensagens'],
            'subject' => $linha['subject'],
            'message' => $linha['message'],
            'enviado' => $linha['enviado']
        );
    }
}else{
    echo 'sem resultado';
}


echo json_encode($respostas);
?>

Follows the structure of the table:

inserir a descrição da imagem aqui

What am I doing wrong?

  • There is no code versioning to see the latest file changes?

  • The importance of a version control my friend!

  • @fernandosavio posted without seeing your comment...rs

  • That’s when the VCS shines

  • It was an accentuation problem....

1 answer

0


It was an accentuation error: it did not list what had accentuation. It was all right.

But I will leave this question open, without closing as solved, to give space to who gives a nice answer.

Browser other questions tagged

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