I was just learning about for loops and stuff and successfully did a FizzBuzz problem, my current code is like so:
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0) {
echo 'FizzBuzz<br>';
} elseif ($i % 3 == 0) {
echo 'Fizz<br>';
} elseif ($i % 5 == 0) {
echo 'Buzz<br>';
} else {
echo $i . '<br>';
}
}
?>
Now I was just wondering what if we have a MySQL table containing 100 rows that have numbers from 1 - 100 inside of them.
CREATE TABLE numbers_from_one_to_hundred (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
numbers INT(11) NOT NULL
);
Now I want to do the FizzBuzz problem with PHP and MySQLi linked with the numbers_from_one_to_hundred.
By the way, I didn't use any prepared statements and stuff in this one because I am bad at them and this is just a pet project so no security worries.
So I did something like this that did not end up working:
$query = "SELECT * FROM `numbers_from_one_to_hundred`";
$results = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($results)) {
$numbers = $row['numbers'];
for ($i = 1; $i <= $numbers; $i++) {
if ($i % 15 == 0) {
echo 'FizzBuzz<br>';
} elseif ($i % 3 == 0) {
echo 'Fizz<br>';
} elseif ($i % 5 == 0) {
echo 'Buzz<br>';
} else {
echo $i . '<br>';
}
}
}
Can someone explain what is wrong with this? It really doesn't output anything at all but when I try print_r with the results it does seem to give the results so something is probably wrong with my logic am I doing it all completely wrong? Can someone explain to me how this should be done?
Add order by numbers
to your query so rows are returned sorted.