MySQL

Display records in a Bootstrap table

Display MySQL data in a Bootstrap table

This example demonstrates how to display MySQL data in a Bootstrap table in a simple and structured way:

YearSong TitleArtist
1992OneU2
2010FireworkKaty Perry
1969Little WomanBobby Sherman
1994Confide in MeKylie Minogue
1990U Can't Touch ThisMC Hammer
1979I Will SurviveGloria Gaynor
1966We Can Work it OutThe Beatles
1960Cradle of LoveJohnny Preston
1956Moonglow & Theme From PicnicMorris Stoloff
2000KryptoniteThree Doors Down
2010Barbra StreisandDuck Sauce
2007No OneAlicia Keys
1957My Special AngelBobby Helms
1982I've Never Been to MeCharlene
1984JoannaKool & The Gang
1985Saving All My Love For YouWhitney Houston
1949Mule TrainTennessee Ernie Ford
1971Mozart Symphony No 40 in G MinorWaldo De Los Rios
1974The Night Chicago DiedPaper Lace
1968Angel of the MorningMerrilee Rush
1995Kiss From a RoseSeal
1999It's All Been DoneBarenaked Ladies
2004My BandD12
1992Am I the Same GirlSwing Out Sister
1966Sunshine SupermanDonovan
1945Along the Navajo TrailDinah Shore
1976Welcome BackJohn Sebastian
1997Karma PoliceRadiohead
1984Hold Me NowThe Thompson Twins
1977Float OnThe Floaters
2007FergaliciousFergie & will.i.am
1946Doctor, Lawyer, Indian ChiefBetty Hutton
1972Too Late To Turn Back NowCornelius Brothers & Sister Rose
2011The Edge Of GloryLady GaGa
1974Spiders & SnakesJim Stafford
1971I Am... I SaidNeil Diamond
1925If You Knew Susie (Like I Know Susie)Eddie Cantor
1955Autumn Leaves (Les Feuilles Mortes)Roger Williams
2008Shake ItMetro Station
1938You Must Have Been a Beautiful BabyBing Crosby
1982The Lion Sleeps TonightTight Fit
1961WheelsString-A-Longs
1996Hit Me OffNew Edition
1988Pink CadillacNatalie Cole
1999No ScrubsTLC
1997Barbie GirlAqua
2009CelebrationMadonna
1981Rock This TownThe Stray Cats
1964It's All Over NowThe Rolling Stones
1955Cool WaterFrankie Laine
1957LucilleLittle Richard
2001Moi... LolitaAlizee
1972Look What You Done for MeAl Green
1979Knock On WoodAmii Stewart
1957Young LoveTab Hunter
1948Powder Your Face With SunshineEvelyn Knight & The Stardusters
1957Wake Up Little SusieThe Everly Brothers
1996WordsBoyzone
2005Beautiful SoulJesse McCartney
1968Cinderella RockefellaEsther & Abi Ofarim
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">
<link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
PHP
<?php
  $conn = new mysqli ('server', 'user', 'password', 'database');
  mysqli_set_charset ($conn, 'utf8');

if ($conn -> connect_error) {
  die ('Connection failed: ' . $conn->connect_error);
  } 

$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_title'] . '</td>';
  echo '<td>' . $row['artist'] . '</td>';
  echo '</tr>';
  }
  
  echo '</tbody>';
  echo '</table>';
  $conn -> close();
  ?>

Responsive Design (Optional)

If you want the table to be responsive, meaning it will adapt to smaller screens, wrap the <table> inside a .table-responsive class:

HTML
<div class="table-responsive">
  <table class="table table-striped"> 

    <!-- Table content goes here -->

  </table>
</div>
Table Custom Settings with jQuery
<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>