PHP is not returning query data

Asked

Viewed 76 times

0

Hello,

I am using the following code to return regions:

$query = 'SELECT * FROM Regiao';
    $json = array();
    $result = mysqli_query ($conn, $query);
    while($row = mysqli_fetch_array ($result))
    {
        $regiao = array(
            'id' => $row['Id'],
            'nome' => $row['Nome'],
        );
        array_push($json, $regiao);
    }

    $jsonstring = json_encode($json);
    echo $jsonstring;

    die();

The result is as follows:

[
    {
        "id": "1",
        "nome": "Norte"
    },
    {
        "id": "2",
        "nome": "Nordeste"
    },
    {
        "id": "3",
        "nome": "Sudeste"
    },
    {
        "id": "4",
        "nome": "Sul"
    },
    {
        "id": "5",
        "nome": "Centro-Oeste"
    }
]

I am using the same code but in another file with the different select:

$query = 'SELECT * FROM Estado';
    $json = array();
    $result = mysqli_query ($conn, $query);
    while($row = mysqli_fetch_array ($result))
    {
        $regiao = array(
            'id' => $row['Id'],
            'codigouf' => $row['CodigoUf'],
            'nome' => $row['Nome'],
            'uf' => $row['Uf'],
            'regiao' => $row['Regiao'],
        );
        array_push($json, $regiao);
    }

    $jsonstring = json_encode($json);
    echo $jsonstring;

    die();

And the result in the second code is in White, does not return me absolutely anything. Could someone please help me? Thank you

  • I noticed that you are using uppercase letters in both table names and column names. You have checked whether the writing is correct?

  • Hi Phelipe, I just checked your question and yes, both table name and column names are correct.

  • Check here carefully and really the code is exactly the same. The only possibility I’m seeing is actually the name of the table fields. Let’s see if anyone else can find some other mistake I didn’t see.

  • If you run this query directly in the database, what is the result?

  • The only difference between the tables is the number of columns, one has 2 the other has 5, and the number of rows, one has 5 and the other 27. But really the table name and columns are correct. Thank you very much, I will wait if someone can help me =)

  • Running this code directly in the database is returned to me the 27 results:

  • Showing 0 - 24 records (27 total, query took 0.0003 seconds.)

Show 2 more comments

1 answer

0


Probably the name column should be filled with accented fields (Ex. Paraná). First, try to remove the column name is see if it returns any results.

If you return try to add in your want the following text as the link: /a/74569

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
  • It worked! I just changed mysql_query("SET NAMES 'utf8'"); to mysql_query($Conn, "SET NAMES 'utf8'"); and it worked.

  • Thank you very much!

Browser other questions tagged

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