Logo Luan Morina
Display MySQL table records in a Bootstrap table
YearSong TitleArtist
1982Der KommissarFalco
1991Motown PhillyBoyz II Men
2005You're BeautifulJames Blunt
2002Like I Love YouJustin Timberlake
1970CottonfieldsThe Beach Boys
2002Jenny From the BlockJennifer Lopez
2004Lean BackTerror Squad
2000Original PranksterOffspring
1983New Year's DayU2
1994Cotton Eye JoeRednex
1973Time in a BottleJim Croce
1978You're the One That I WantJohn Travolta & Olivia Newton-John
1998The Rockafeller SkankFatboy Slim
1948It's MagicDoris Day
1978Who Are You?The Who
1999You'll Be in My HeartPhil Collins
1995Tell MeGroove Theory
1998Together AgainJanet Jackson
1953Eh, cumpariJulius Larosa
2002HeavenDJ Sammy & Yanou
1984My Oh MySlade
1976Theme From 'Mahogany' (Do You Know Where You're Going To)Diana Ross
1967Ruby TuesdayThe Rolling Stones
1926ValenciaPaul Whiteman
1963Ruby BabyDion
1966God Only KnowsThe Beach Boys
1999If I Could Turn Back the Hands of TimeR Kelly
1984Wild BoysDuran Duran
1970Mademoiselle NinetteSoulful Dynamics
1965Wooly BullySam The Sham & The Pharaohs
1996IronicAlanis Morissette
1989The LookRoxette
1996If it Makes You HappySheryl Crow
1968Piece of My HeartBig Brother & the Holding Company
1963Da Doo Ron Ron (When He Walked Me Home)The Crystals
1959Only SixteenSam Cooke
2007Love TodayMika
1976Fooled Around & Fell in LoveElvin Bishop
1987Little LiesFleetwood Mac
1938Change PartnersFred Astaire
1972Look What You Done for MeAl Green
1975Pick Up the PiecesAverage White Band
1972Mother & Child ReunionPaul Simon
1941We Three (My Echo, My Shadow & Me)The Ink Spots
1994All I Wanna DoSheryl Crow
1998Too MuchSpice Girls
1985Dancing in the StreetDavid Bowie & Mick Jagger
2007Shut Up & DriveRihanna
1965I'm Henry the Eighth I AmHerman's Hermits
1997Tic Tic Tac (Dance To Boi Bumba!)Carrapicho & Chilli
1976December 1963 (Oh What a Night)The Four Seasons
2005Beautiful SoulJesse McCartney
1954I Get So Lonely (When I Dream About You)The Four Knights
1995Strong EnoughSheryl Crow
1967I Was Kaiser Bill's BatmanWhistling Jack Smith
1994100% Pure LoveCrystal Waters
1985I Got You BabeUB40 & Chrissie Hynde
1969HairThe Cowsills
1958La BambaRitchie Valens
2001Thank YouDido

Link to Bootstrap CSS and JS Files

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"></script>

Link to Bootstrap dataTables CSS and JS Files

<link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css">
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>

PHP

<?php
  $conn = new mysqli ("server", "username", "password", "database-name");
  mysqli_set_charset ($conn, "utf8"); 
  
  $sql = "SELECT * FROM top_5000_songs ORDER BY RAND() LIMIT 0, 60";
  $result = $conn -> query($sql);
  
  echo "<table id=\"table-demo\" class=\"table table-striped\">";
  echo "<thead>";
  echo "<tr>";
  echo "<th>Year</th>";
  echo "<th>Song Title</th>";
  echo "<th>Artist</th>";
  echo "</tr>";
  echo "</thead>";
  echo "<tbody>"; 
  while ($row = $result -> fetch_assoc()) { 

  echo "<tr>";
  echo "<td>" . $row["year"] . "</td>";
  echo "<td>" . $row["song"] . "</td>";
  echo "<td>" . $row["artist"] . "</td>";
  }
  
  echo "</tr>";
  echo "</tbody>";
  echo "</table>";
  $conn -> close();
  ?>

JS

<script>
$(document).ready(function() {
$("#table-demo").DataTable({
paging: true,
pageLength: 4,
ordering: true,
info: false,
lengthChange: false,
language: {
search: "_INPUT_",
searchPlaceholder: "Search"
},
search: {
addClass: 'form-control input-lg col-xs-12',
},
});
});
</script>
   Display table records in a Bootstrap table