JSON does not return Mysql database data

Asked

Viewed 53 times

0

I made a very simple application to search for projects by state in the database. However, JSON does not show any errors, but also does not return anything. I imagine the problem may be in GET. Do you know what it might be? Follow the codes below:

HTML form code:

<div style="margin-left:100px; margin-top:60px">
        <h2>Buscar projetos por estado</h2><br><br>
        <h4>Escolha o estado desejado: </h4><br/>
        <form action="consultarEstado.php" method="get">
        <select name="estado" id="estado">
            <option value="AC">Acre</option>
            <option value="AL">Alagoas</option>
            <option value="AP">Amapá</option>
            <option value="AM">Amazonas</option>
            <option value="BA">Bahia</option>
            <option value="CE">Ceará</option>
            <option value="DF">Distrito Federal</option>
            <option value="ES">Espírito Santo</option>
            <option value="GO">Goiás</option>
            <option value="MA">Maranhão</option>
            <option value="MT">Mato Grosso</option>
            <option value="MS">Mato Grosso do Sul</option>
            <option value="MG">Minas Gerais</option>
            <option value="PA">Pará</option>
            <option value="PB">Paraíba</option>
            <option value="PR">Paraná</option>
            <option value="PE">Pernambuco</option>
            <option value="PI">Piauí</option>
            <option value="RJ">Rio de Janeiro</option>
            <option value="RN">Rio Grande do Norte</option>
            <option value="RS">Rio Grande do Sul</option>
            <option value="RO">Rondônia</option>
            <option value="RR">Roraima</option>
            <option value="SC">Santa Catarina</option>
            <option value="SP">São Paulo</option>
            <option value="SE">Sergipe</option>
            <option value="TO">Tocantins</option>
        </select>
        <input type="submit" value="Buscar">
        </form>
        </div>

Building the JSON:

<?php

include './Classes/Conexao.php';
include './Classes/DAO/UsuarioDAO.php';

if ($_GET)) {
    $estado = $_GET['estado'];
    $UsuarioDAO = new UsuarioDAO();
    $consulta = $UsuarioDAO->consultarEstado($estado);

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

            $respostas [] = array (
                'id_projeto' => $linha['id_projeto'],
                'municipio' => $linha['municipio'],
                'estado' => $linha['estado'],
                'nome' => $linha['nome'],
            );   
        }
    }

echo json_encode($respostas);
} else {
    echo 'Nenhum parâmetro foi passado na URL.';
}

?>

Function:

public function consultarEstado($estado) {
        $sql = "SELECT id_projeto, municipio, estado, nome FROM projeto WHERE estado = '$estado'";

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

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

2 answers

0

You are passing the state via post and are trying to read with get, try:

<?php

include './Classes/Conexao.php';
include './Classes/DAO/UsuarioDAO.php';

if ($_POST)) {
    $estado = $_POST['estado'];
    $UsuarioDAO = new UsuarioDAO();
    $consulta = $UsuarioDAO->consultarEstado($estado);

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

            $respostas [] = array (
                'id_projeto' => $linha['id_projeto'],
                'municipio' => $linha['municipio'],
                'estado' => $linha['estado'],
                'nome' => $linha['nome'],
            );   
        }
    }

echo json_encode($respostas);
} else {
    echo 'Nenhum parâmetro foi passado na URL.';
}

?>

Or change your form method to GET in your HTML file.

  • The problem persists. I had already done the test with GET and POST and the two continue without returning me anything.

0

I discovered that the problem was being caused by the special characters of the data. I put utf8_encode in each line of the vector and managed to return the values, although the special characters still need to be treated.

<?php

include './Classes/Conexao.php';
include './Classes/DAO/UsuarioDAO.php';

if ($_GET) {
    $estado = $_GET['estado'];
    $UsuarioDAO = new UsuarioDAO();
    $consulta = $UsuarioDAO->consultarEstado($estado);

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

            $respostas [] = array (
                'id_projeto' => utf8_encode($linha['id_projeto']),
                'municipio' => utf8_encode($linha['municipio']),
                'estado' => utf8_encode($linha['estado']),
                'nome' => utf8_encode($linha['nome']),
            );   
        }
    }

echo json_encode($respostas);
} else {
    echo 'Nenhum parâmetro foi passado na URL.';
}

?>
  • Okay, but you shouldn’t have to do this. Possible problems: your HTML is not being served as UTF-8, or your form is not being posted as UTF-8, or the encoding of the connection to the database is not being set, or the collation of your database table is wrong.

Browser other questions tagged

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