How to Catch Results inside the loop and grouping into a variable outside it

Asked

Viewed 1,000 times

1

How to Catch Results inside the loop and grouping into a variable outside it Example:

$sql55 = "SELECT * FROM finan WHERE MONTH(data_fechamento)= '02' ";
$resultado55 = mysql_query($sql55) or die( mysql_error());
while ($row55 = mysql_fetch_assoc($resultado55)) {
echo '<br>';
echo $vencimento2 =  $row55['vencimento'];
//  retorno echo  2017-02-02
                  2017-02-04
                  2017-02-05
                  2017-02-03
}

// queria pegar esse mesmo daros em uma variaval fora do while
echo $juntandoresultados; // exmplo

//////////////////////////////////////////////////////////////////////

2 answers

2

$sql55 = "SELECT * FROM finan WHERE MONTH(data_fechamento)= '02' ";
$resultado55 = mysql_query($sql55) or die( mysql_error());
$juntandoresultados="";
while ($row55 = mysql_fetch_assoc($resultado55)) {

$juntandoresultados .= "<br>".$row55['vencimento'];

}
echo $juntandoresultados;
  • For your answer to be perfect is only missing put $joining results=""; before the while. 'Cause the way it is, spending the first time in the while will make an error that the variable $joining results does not exist.

  • 1

    beauty, I’ve already put

  • Mark the answer as chosen :)

1

Saving inside an array would help?

$dados = array();

$sql55 = "SELECT * FROM finan WHERE MONTH(data_fechamento)= '02' ";
$resultado55 = mysql_query($sql55) or die( mysql_error());
while ($row55 = mysql_fetch_assoc($resultado55)) {
  array_push($dados,$row55['vencimento']);
}

print_r($dados);

----------------------or so ------

$dados = "";

$sql55 = "SELECT * FROM finan WHERE MONTH(data_fechamento)= '02' ";
$resultado55 = mysql_query($sql55) or die( mysql_error());
while ($row55 = mysql_fetch_assoc($resultado55)) {
  $dados .= $row55['vencimento']." ";
}

echo $dados ;
  • I believe your second option solves the problem the way he wants. Only instead of spacing at the end it’s concatenating with <br> in front.

Browser other questions tagged

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