I had an API returning the data correctly, but it stopped popping up.
The SQL statement when executed in Wrockbrench works. The other APIs follow this same pattern below. But it is not coming back. And I was returning before, I do not know what I changed that shows nothing of the result. Here are 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 Return API with JSON:
<?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);
?>
The table structure follows:
What am I doing wrong?