MySQL

Display records in a Bootstrap table

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

YearSong TitleArtist
1965It's Not UnusualTom Jones
1978Flash LightParliament
1940New San Antonio RoseBob Wills & his Texas Playboys
2011Mr SaxobeatAlexandra Stan
1973Smoke On the WaterDeep Purple
1969JeanOliver
1958Bird DogThe Everly Brothers
1978Mary's Boy Child/Oh My LordBoney M
1988How Can I FallBreathe
1994Basket CaseGreen Day
1987BreakoutSwing Out Sister
1961Spanish HarlemBen E King
1954Make Love to MeJo Stafford
1966Yesterday ManChris Andrews
1964Hippy Hippy ShakeSwinging Blue Jeans
1954Hernando's HideawayArchie Bleyer
1990Get Up (Before the Night is Over)Technotronic
1959DonnaRitchie Valens
2007Sweetest Girl (dollar Bill)Wyclef Jean & Akon & Lil' Wayne
1997For You I WillMonica
2004Lose My BreathDestiny's Child
1951Dust My BroomElmore James
1950If I Knew You Were Comin' I'd Have Baked a CakeEve Young
2004GoodiesCiara & Petey Pablo
2007Mr BartenderT-Pain
1999Baby One More TimeBritney Spears
2000Go Let it OutOasis
1994Mmm Mmm Mmm MmmCrash Test Dummies
1946Ole Buttermilk SkyKay Kyser
1958Do You Want to Dance?Bobby Freeman
1969Bad Moon RisingCreedence Clearwater Revival
1974Eres Tu (Touch The Wind)Mocedades
1970All Right NowFree
1996Nobody KnowsTony Rich Project
1976Love Really Hurts Without YouBilly Ocean
1998Ray of LightMadonna
2009Gives You HellAll-American Rejects
196696 TearsQuestion Mark & The Mysterians
1990Downtown TrainRod Stewart
1986Higher LoveSteve Winwood
2005StickwituThe Pussycat Dolls
2012We Are Youngfun.
2010AirplanesBoB & Hayley Williams
200321 Questions50 Cent
2004Dry Your EyesThe Streets
2010Raise Your GlassPink
1955Tutti FruttiLittle Richard
1968WorldBee Gees
1952Botch-a-me (Ba-ba-baciami piccina)Rosemary Clooney
1968Lady WillpowerGary Puckett & The Union Gap
2000MusicMadonna
1985Alive & KickingSimple Minds
1959El PasoMarty Robbins
1980Do That to Me One More TimeCaptain & Tennille
1965I'm Henry the Eighth I AmHerman's Hermits
1985You're the InspirationChicago
1987Caravan of LoveThe Housemartins
2005Lonely No MoreRob Thomas
1995Let Her CryHootie & The Blowfish
2001Somethin' StupidRobbie Williams & Nicole Kidman

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:

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

    <!-- Table content goes here -->

  </table>
</div>

JS

<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>