Variable receiving only 1 single fetch value, how to fix?

Asked

Viewed 141 times

1

I need to pass all values of the column to a variable, for example:

Key -> $key;

Content -> $Content;

So I used bindColumn, but at the time that fetch() is performing it is only bringing 1 result, and I need all, I tried to use fetchAll() but the same, how to go all values, not only 1.

My code currently:

<?php

$pdo = new PDO("mysql:host=localhost;dbname=73519225000122", "root", "");
$buscar = $pdo->prepare("SELECT chave, conteudo FROM xml WHERE modelo = '55' LIMIT 100 ");
$buscar->execute();
$buscar->bindColumn(1, $chave);
$buscar->bindColumn(2, $conteudo);
$linha = $buscar->fetch();

echo json_encode($chave);

2 answers

1


You should fetch all data from the SQL query using, for example, the PDO fetch (but it could be another too), so: you can concatenate an entire column into a single variable, within the loop, in which case the while. To concatenate some column of your table, within the while, you use a stitch ..

In your case, stay:

       $chave_2 = "", $conteudo_2 = "";
       while ($linha = $buscar->fetch(PDO::FETCH_ASSOC)) {
             //Concatenação (.=):
             $chave_2 .=  $linha['chave'];
             //Concatenação (.=):
             $conteudo_2 .=  $linha['conteudo'];
       }

Remembering that the function fetch(PDO::FETCH_ASSOC) with the attribute PDO::FETCH_ASSOC "returns a matrix indexed by column name, as returned in its result set"

Source: fetch(PDO::FETCH_ASSOC)

Try to follow him:

     $pdo = new PDO("mysql:host=localhost;dbname=73519225000122", "root", "");
        $buscar = $pdo->prepare("SELECT chave, conteudo FROM xml WHERE modelo = '55' LIMIT 100 ");
        $buscar->execute();
        $buscar->bindColumn(1, $chave);
        $buscar->bindColumn(2, $conteudo);

       /* $chave = [], $conteudo = [];
         while ($linha = $buscar->fetch(PDO::FETCH_ASSOC)) {
              array_push($chave, $linha['chave']);
              array_push($conteudo, $linha['conteudo']);
            }

       var_dump(json_encode($chave));
       var_dump(json_encode($conteudo));*/

      //Esta parte deve resolver pra você:

       $chave_2 = "", $conteudo_2 = "";
       while ($linha = $buscar->fetch(PDO::FETCH_ASSOC)) {
             $chave_2 .=  $linha['chave'];
             $conteudo_2 .=  $linha['conteudo'];
       }

       var_dump($chave_2);
       var_dump($conteudo_2);
  • It worked, but let me ask you, how would I leave all the records on top of the $key variable and all the contents on top of the $content variable? If I wanted to generate files from the contents, I can define the name of each file with the value that comes from the $key variable, because they would run together; Type line -> key:[11111111111111111111111]; line -> content:[11111111111111111111111111111111111111]; Logo.

  • I’ll edit the answer.

  • Ahhhh now intendi how to do, Because Taffa, thanks even for the help man! Thank you very much, you do not know how much I was beating my head to solve this, I did a lot of research on google and in facebook groups before posting here and you helped me damn well, thanks!

  • Don’t forget to vote and accept the answer. [kkkk]

  • I’m new to arch kkk but I’ve done it!

1

The fetch only picks up one line at a time, it was made to be used in loops. It seems that you want the behavior of fetchAll. But avoid using fetchAll if it’s too much data.

  • I used fetchAll but also continued to bring only 1 record. :/

Browser other questions tagged

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