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.
– Antonio Dvino
I’ll edit the answer.
– Taffarel Xavier
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!
– Antonio Dvino
Don’t forget to vote and accept the answer. [kkkk]
– Taffarel Xavier
I’m new to arch kkk but I’ve done it!
– Antonio Dvino