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
1934June in JanuaryBing Crosby
1989Listen to Your HeartRoxette
1966Yesterday ManChris Andrews
1986The Next Time I FallPeter Cetera & Amy Grant
1972Black & WhiteThree Dog Night
2006Over My Head (Cable Car)The Fray
1987In Too DeepGenesis
1984Miss Me BlindCulture Club
1988Alphabet StreetPrince
1951Longing For YouTeresa Brewer
1981Morning Train (Nine to Five)Sheena Easton
1977You Make Loving FunFleetwood Mac
1985KayleighMarillion
1995Scatman's WorldScatman John
1973AngieThe Rolling Stones
2005Don't Phunk With My HeartThe Black Eyed Peas
2010Cooler than meMike Posner
1999Back At OneBrian McKnight
2005Hung UpMadonna
1959Mack the KnifeBobby Darin
1967JacksonNancy Sinatra & Lee Hazlewood
1966I am a RockSimon & Garfunkel
1909Swing Low, Sweet ChariotFisk University Jubilee Quartet
1973Drift AwayDobie Gray
1944Is You Is or Is You Ain't (Ma' Baby)Louis Jordan
1989The LookRoxette
2001JadedAerosmith
1977DreamsFleetwood Mac
1952You Belong to MeJo Stafford
1978Wonderful TonightEric Clapton
1982Eye in the SkyThe Alan Parsons Project
2005Trippin'Robbie Williams
1979Bad Case of Loving YouRobert Palmer
1992Rump ShakerWreckx-N-Effect
2002Hands CleanAlanis Morissette
1975Una Paloma BlancaGeorge Baker Selection
1975Lady Marmalade (Voulez-Vous Coucher Aver Moi Ce Soir?)Patti LaBelle
1986Spirit in the SkyDoctor & The Medics
1989Born to Be My BabyBon Jovi
1965Over & OverDave Clark Five
1984Let's Hear it For the BoyDeniece Williams
2003Rock Wit U (Awww Baby)Ashanti
1954Cara MiaDavid Whitfield & Mantovani
1981AntmusicAdam & The Ants
1991ChangeLisa Stansfield
1956True LoveBing Crosby & Grace Kelly
1990Thieves in the TemplePrince
2000Could I Have This Kiss ForeverWhitney Houston & Enrique Iglesias
1978Dreadlock Holiday10CC
1996NameThe Goo Goo Dolls
1984FootlooseKenny Loggins
1977God Save the QueenThe Sex Pistols
2006Me & UCassie
1988Foolish BeatDebbie Gibson
1993HeroMariah Carey
1987Boys (Summertime Love)Sabrina
1988I Will Always Love YouTaylor Dayne
2002PerdonoTiziano Ferro
1991Wicked GameChris Isaak
1964Remember (Walkin' in the Sand)The Shangri-Las
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>