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
1976Devil WomanCliff Richard
1942Blues in the NightWoody Herman
2000Maria MariaSantana
1934Cocktails For TwoDuke Ellington
1992I'll Be ThereMariah Carey
1995Be My LoverLa Bouche
2010MineTaylor Swift
1998Sex & CandyMarcy Playground
1968On the Road AgainCanned Heat
1981Theme From 'Greatest American Hero' (Believe It Or Not)Joey Scarbury
1981Prince CharmingAdam & The Ants
1979ReunitedPeaches & Herb
2002Gimme the LightSean Paul
1936Goody GoodyBenny Goodman
1982Eye of the TigerSurvivor
1982Heart AttackOlivia Newton-John
1923Yes! We Have No BananasBilly Jones
2001Fallin'Alicia Keys
1962Crying in the RainThe Everly Brothers
1993The River of DreamsBilly Joel
1956See You Later AlligatorBill Haley & his Comets
1966Stop Stop StopThe Hollies
1985Walking On SunshineKatrina & The Waves
1970Don't Cry DaddyElvis Presley
1949Cruising Down the RiverRuss Morgan
1965The Birds & the BeesJewel Akens
1971Stairway to HeavenLed Zeppelin
1991Should I Stay Or Should I GoThe Clash
20007 DaysCraig David
1920SwaneeAl Jolson
1969Little WomanBobby Sherman
1978Mary's Boy Child/Oh My LordBoney M
2006Me & UCassie
1988Doctorin' the TardisKLF
1984Stuck On YouLionel Richie
2001I Hope You DanceLee Ann Womack
2002I'm Gonna Getcha Good!Shania Twain
1995Hand in My PocketAlanis Morissette
1994Breathe AgainToni Braxton
2002Get the Party StartedPink
1954Cara MiaDavid Whitfield & Mantovani
1954The Little ShoemakerThe Gaylords
1975Low RiderWar
2001Drops of Jupiter (Tell Me)Train
1942White ChristmasBing Crosby
1997Anybody Seen My BabyThe Rolling Stones
1972Listen to the MusicThe Doobie Brothers
1993Give it UpCut 'N' Move
1986I Miss YouKlymaxx
1994I'll Stand By YouThe Pretenders
1984Caribbean Queen (No More Love On the Run)Billy Ocean
1981One of UsAbba
1999Last KissPearl Jam
2003Can't Hold Us DownChristina Aguilera
1923Down Hearted BluesBessie Smith
1968Hurdy Gurdy ManDonovan
1983Making Love (Out of Nothing At All)Air Supply
2002The Zephyr SongRed Hot Chili Peppers
1998The Sweetest ThingU2
1983Save Your LoveRenee & Renato
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>