MySQL

Display records in a Bootstrap table

Here is a simple example of displaying MySQL data in a Bootstrap table:

YearSong TitleArtist
1971Go Away Little GirlDonny Osmond
1971I Beg Your Pardon (I Never Promised You a Rose Garden)Lynn Anderson
1979One Way TicketEruption
1984Sounds Like a MelodyAlphaville
2003Anyone of Us (Stupid Mistake)Gareth Gates
1950The ThingPhil Harris
1974Devil Gate DriveSuzi Quatro
1995You Oughta KnowAlanis Morissette
1976The Rubberband ManThe Detroit Spinners
1998Walkin' On the SunSmash Mouth
2001Smooth CriminalAlien Ant Farm
2006Waiting on the World to ChangeJohn Mayer
1977Mull of KintyreWings
1980The Second Time AroundShalamar
1979I Was Made For Lovin' YouKiss
1991Love is a Wonderful ThingMichael Bolton
2006Suddenly I SeeKT Tunstall
2003IgnitionR Kelly
1986There'll Be Sad Songs (To Make You Cry)Billy Ocean
1983ManiacMichael Sembello
1980The RoseBette Midler
1920SwaneeAl Jolson
2008Live Your LifeT.I. & Rihanna
1971She's a LadyTom Jones
1985Just Another NightMick Jagger
2005IncompleteThe Backstreet Boys
1985Material GirlMadonna
1986Living DollCliff Richard & The Young Ones
1982The Land of Make BelieveBucks Fizz
1989I Don't Wanna Lose YouGloria Estefan
1974Tiger FeetMud
1994AlwaysBon Jovi
1941StardustArtie Shaw
1999That Don't Impress Me MuchShania Twain
2005CoolGwen Stefani
1989Love TrainHolly Johnson
1988I Think We're Alone NowTiffany
2001In the EndLinkin Park
1949That Lucky Old Sun (Just Rolls Around Heaven All Day)Frankie Laine
1993I Have NothingWhitney Houston
1972Son of My FatherChicory Tip
1927Me & My ShadowJack Smith
1968White RoomCream
1979I Got My Mind Made UpInstant Funk
1979We Are FamilySister Sledge
2006Pump ItThe Black Eyed Peas
1964Under the BoardwalkThe Drifters
1998IntergalacticThe Beastie Boys
1974Living For the CityStevie Wonder
1961Little SisterElvis Presley
1994RegulateWarren G & Nate Dogg
2006The Saints are ComingU2 & Green Day
2004Jesus WalksKanye West
1929HoneyRudy Vallee & his Connecticut Yankees
1993Mr VainCulture Beat
1985The Power of LoveHuey Lewis & The News
1971Double BarrelDave & Ansil Collins
1958Stupid CupidConnie Francis
2009I Know You Want Me (calle Ocho)Pitbull
1959RouletteRuss Conway
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>