MySQL

Display records in columns

Here's an example of how to display MySQL database records in Bootstrap columns.

A Whiter Shade of Pale
Procol Harum (1967)

A Woman in Love
Barbra Streisand (1980)

All That She Wants
Ace of Base (1993)

Angie
The Rolling Stones (1973)

Another Day in Paradise
Phil Collins (1989)

Baby One More Time
Britney Spears (1999)

Barbie Girl
Aqua (1997)

Believe
Cher (1998)

Bette Davis Eyes
Kim Carnes (1981)

PHP

<div class="container">
<div class="row justify-content-between">
<?php
$conn = new mysqli ("server", "user", "password", "database");
mysqli_set_charset ($conn, "utf8");

if ($conn -> connect_error) {
die ("Error: " . $conn->connect_error);
}

$sql = "SELECT * FROM top_100_songs_eu ORDER BY song ASC LIMIT 0, 9";
$result = $conn -> query($sql);
if ($result -> num_rows > 0) {

while ($row = $result -> fetch_assoc()) { 
$row_song_title[] = '<strong>' . $row["song"] . '</strong><br />' . $row["artist"] . ' (' . $row["year"] . ')<br /><br />'; 
$songs_list = array_chunk($row_song_title, ceil(count($row_song_title) / 3));
}

foreach ($songs_list as $songs_column) {
echo '<div class="col-md-4">';
foreach ($songs_column as $song_output) { 
echo $song_output;
}

echo "</div>";
} 
} 

else {
echo "No records found";
}

$conn -> close();
?>
</div>
</div>