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
1996Coco JambooMr President
1981Slow HandThe Pointer Sisters
1990Come Back to MeJanet Jackson
1958A Certain SmileJohnny Mathis
1988Im Nin'AluOfra Haza
19989pm (Till I Come)ATB
1999If You Had My LoveJennifer Lopez
2004Just Lose ItEminem
1986All I Need is a MiracleMike & The Mechanics
2010Waka Waka (This Time For Africa)Shakira & Freshly Ground
1984Miss Me BlindCulture Club
1961Daddy's HomeShep & the Limelites
1947That's My DesireFrankie Laine
1989Every Rose Has It's ThornPoison
1969I'll Never Fall in Love AgainTom Jones
1984GhostbustersRay Parker Jr
1998My Favourite MistakeSheryl Crow
1969(Call Me) Number OneThe Tremeloes
1967It Must Be Him (Seul Sur Son Etoile)Vikki Carr
1986Manic MondayThe Bangles
1960Teen AngelMark Dinning
1973Also Sprach Zarathustra (2001)Eumir Deodato
1990KillerAdamski
1960Mule Skinner BluesFendermen
1976Theme from 'SWAT'Rhythm Heritage
1998When the Lights Go OutFive
1976Baby, I Love Your WayPeter Frampton
1972LaylaDerek & The Dominos
1955Heart of StoneThe Fontane Sisters
1996C'Mon Ride the TrainQuad City DJs
1927Someone to Watch Over MeGertrude Lawrence
1985St Elmo's Fire (Man in Motion)John Parr
1984Do They Know It's Christmas?Band Aid
1987Heart & SoulT'Pau
1973Nutbush City LimitsIke & Tina Turner
1998GoodbyeSpice Girls
2003Lifestyles of the Rich & FamousGood Charlotte
1998From This Moment OnShania Twain
1965Wooly BullySam The Sham & The Pharaohs
1986Addicted to LoveRobert Palmer
19961,2,3,4 (Sumpin' New)Coolio
1960GreenfieldsBrothers Four
1980Tired of Toein' The LineRocky Burnett
2008Love LockdownKanye West
1962Don't Break the Heart That Loves YouConnie Francis
1997HoneyMariah Carey
1949Forever & EverRuss Morgan
1977We Are the ChampionsQueen
1966What Becomes of the Broken HeartedJimmy Ruffin
2001What it Feels Like For a GirlMadonna
1995Army of MeBjork
2001ButterflyCrazy Town
1985Money For NothingDire Straits
2004Jesus WalksKanye West
1926I'm Sitting On Top of the WorldAl Jolson
2006I Don't Feel Like Dancin'The Scissor Sisters
2012DominoJessie J
1979Some GirlsRacey
1981Jealous GuyRoxy Music
1967Waterloo SunsetThe Kinks
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>