I need to keep my variable in long form, I don't understand these changes.
my code:
//DB CONNECTION
$sql = "SELECT * FROM `slevy` WHERE 1 ";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$sleva = $row['sleva'];
echo $sleva;
}
echo "------------".$sleva;
output is:
99381585659209563465667261720583722336659395405------------659395405
In the body of cycle you save into $sleva
and then echo
all sales, after the while
cycle $sleva
contains just the last one row from database.
while ($row = $result->fetch_assoc()) {
$sleva = $row['sleva'];
echo $sleva . '<br>';
}
// output like:
993815856<br>
592095634<br>
656672617<br>
205837223<br>
659395405<br>
You wrote to keep variable in long form
. It means, probably, to save into $sleva
variable the whole string, concatenate all sales.
$sleva = '';
while ($row = $result->fetch_assoc()) {
$sleva .= $row['sleva'];
}
echo $sleva;
// output: 99381585659209563465667261720583722336659395405
You are echoing each value you get in while loop separately, so at the end the variable contains only the last value. To assign all those values in one string to the variable, you need to contatenate it like this:
$sleva.= $row['sleva'];
Also remember to initialize it before the loop:
$sleva = '';
If you want $sleva
to contain the whole string use $sleva .= $row['sleva'];
to add "content" to the variable. In your code, every iteration of the while
loop sets $sleva
with new content.