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
1985Everything She WantsWham!
1970Up Around the BendCreedence Clearwater Revival
1983Red Red WineUB40
2004Move Ya BodyNina Sky
1969Boom Bang-A-BangLulu
1980Sexy EyesDr Hook
1994The Real Thing2 Unlimited
1971Immigrant SongLed Zeppelin
1996Tha CrossroadsBone Thugs-n-Harmony
1994Bump N' GrindR Kelly
1963Little Town FlirtDel Shannon
1963Maria ElenaLos Indios Tabajaras
1970Cracklin' RosieNeil Diamond
2011Mr SaxobeatAlexandra Stan
1956True LoveBing Crosby & Grace Kelly
1985Rhythm of the NightDeBarge
2012Take CareDrake & Rihanna
1985You're My Heart You're My SoulModern Talking
2002DirrtyChristina Aguilera & Redman
1944Shoo-Shoo BabyThe Andrews Sisters
1989I'll Be There For YouBon Jovi
1955Softly SoftlyRuby Murray
1966Ain't Too Proud to BegThe Temptations
1926ValenciaPaul Whiteman
2003ChihuahuaDJ BoBo
2010Take It OffKe$ha
1960Walk Don't RunThe Ventures
1955Bo DiddleyBo Diddley
1977Gonna Fly Now (Theme From 'Rocky')Bill Conti
1941I Don't Want to Set the World On FireHorace Heidt
1966Monday MondayThe Mamas & The Papas
1964I Saw Her Standing ThereThe Beatles
2008Just DanceLady GaGa & Colby O'Donis
1997DiscothequeU2
1975Love is the DrugRoxy Music
1983Reet PetiteJackie Wilson
1979Goodnight TonightWings
1963Surfin' USAThe Beach Boys
2004I'm Still in Love With YouSean Paul
1966No Milk TodayHerman's Hermits
1983Islands in the StreamKenny Rogers & Dolly Parton
2007Do You Know (The Ping Pong Song)Enrique Iglesias
197650 Ways to Leave Your LoverPaul Simon
1988The Way You Make Me FeelMichael Jackson
1958Summertime BluesEddie Cochran
1982Come On EileenDexys Midnight Runners
1934Love in BloomBing Crosby
1992If You Asked Me ToCeline Dion
1964Ain't That Lovin' You BabyElvis Presley
1976More Than a FeelingBoston
1957Wake Up Little SusieThe Everly Brothers
1991Let's Talk About SexSalt-N-Pepa
1967Hey JoeJimi Hendrix
2002Stop Crying Your Heart OutOasis
1981UrgentForeigner
1996Virtual InsanityJamiroquai
1991Black Or WhiteMichael Jackson
1977Strawberry Letter 23Brothers Johnson
1978Fool (If You Think It's Over)Chris Rea
2004Sick & TiredAnastacia
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>