Assign results from a WHILE to a Variable

1

guys, I have the following case: I have the following code:

 $sql ="SELECT * FROM monto LIMIT $limite_base, $l ";
$resultadosql = mysqli_query($db, $sql);
while ($rowsql=mysqli_fetch_row($resultadosql)){
  printf ("%s\nBsS\n", $rowsql[1]);
  $montos_permitidos = $rowsql[1];
}

Practically what I want is that the variable $ amounts_allowed is equal to the output that printf gives me but I have not managed to do it. If I leave it like this the variable $ amounts allowed allows me to output the last data of my Database. The values in my table are something like this:

id  monto
1   20
2   30
3   40
4   50
5   60

For me it was ideal that the output was something similar to:

20 BsS, 30 BsS y 40 BsS

Thanks in advance for the help you can give me.

    
asked by Jose M Herrera V 28.10.2018 в 08:56
source

1 answer

2

If what you need is for the variable to accumulate the data you have to concatenate the answer you want.

The easiest way to understand that is this example:

     $sql ="SELECT * FROM monto LIMIT $limite_base, $l ";
$resultadosql = mysqli_query($db, $sql);
$num_datos = mysqli_num_rows($resultadosql);
while ($rowsql=mysqli_fetch_row($resultadosql)){
    if($num_datos ==1)
    {
       $montos_permitidos = $montos_permitidos . "y " . $rowsql[1] . " BsS";
    }
    else
    {
       //printf ("%s\nBsS\n", $rowsql[1]);
       $montos_permitidos = $montos_permitidos . $rowsql[1] . " BsS, ";
    }
    $num_datos--;
}
    
answered by 28.10.2018 / 13:04
source