Json does not return data from Mysql database

Asked

Viewed 335 times

0

I have a table in the database Mysql which contains 3 fields ( id, name and template) different from the other fields, the email template data with tags HTML (structure in longtext).

When I execute the code PHP to return the data, does not return any information. when I run the same script in another table, the data are returned perfectly. I believe it is the data with tags HTML that exists in my model.

Below follows all my script PHP:

    <?php

include_once "conexao.php";

$sql = $db->prepare("SELECT * FROM modelo_email");          
$sql->execute();
while($data = $sql->fetchAll( PDO::FETCH_ASSOC )){
    $json ["data"] = $data;

}

 echo json_encode($json);

2 answers

1

It is because you reversed the positions while storing the information in the variable.

 <?php

include_once "conexao.php";

$sql = $db->prepare("SELECT * FROM modelo_email");          
$sql->execute();
while($data = $sql->fetchAll( PDO::FETCH_ASSOC )){

   //Abaixo você cria a variável e armazena o conteúdo do array nela; 
   $json = $data["data"];

}

 echo json_encode($json);
  • It returns the value like this now "null"

  • Try to display only with echo $json or better still use var_dump($json) to see what is being stored inside the $json variable.

  • with var_dump displays the data. but brings with character error, I’m thinking that the error would be this.

  • array(5) { [0]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(25) "Cobran Letter to Simple" ["model"]=> string(1440) " Dear (a) Lord (a) Checking our files, We do not identify the payment of the (s) quota(s) listed below (s).

  • In the example you sent me, I am not seeing the "date" field that you are trying to bring inside the variable To bring the ID fieldVoce has to do $json = $data['id']; $json = $data['name']; $json = $data['template']; Basically this means, $json= (I want the $json variable to receive) from the SELECT output that is stored in the array $data the field [fieldname].

  • I’m not saying that the use of the ['data'] field is incorrect, because you could have this 'date' field in the MODELO_EMAIL table as for example to store the registration date. In order to be able to certify that this field exists, run a var_dump($data). But I believe the problem is that you can bring a field name that does not exist in your table in case [date].

  • Worse than putting the data, but still it returns null.. when I give a var_dump it returns me the full array , with all the data that exists in the table

Show 2 more comments

1


In select you are not giving the names of the table fields you want to play in the array.

$sql = $db->prepare("select id, descricao, modelo_email from modelo_email");

Here you should use fetch and not fetchAll because no while is hoping to bring one piece of information at a time and fetchAll brings a set of array

while($data = $sql->fetch(PDO::FETCH_ASSOC)){

You can use fetchAll but in this case recommend using with foreach.

In this last part you are wanting to echo off while and that is why you are returning null. If you echo inside the while it will be displayed correctly

while($data = $sql->fetch(PDO::FETCH_ASSOC)){
       {
             echo $json = $data['descricao'];
             // var_dump($json);
        }

I don’t know much about php, but by searching the internet, if you want to display the while result outside of it, you can declare a variable as global.

foreach($data = $sql->fetchAll(PDO::FETCH_ASSOC) as $data1)
    {
         $_global= $json = $data1['descricao'];
         // var_dump($json);
    }
       echo $json;

More like I said, I don’t understand almost anything about PHP, it’s only been 10 days since I entered this endeavor, so I advise you to do a search on global variables before making use of them.

Browser other questions tagged

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