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
1969Spinning WheelBlood Sweat & Tears
1967San Francisco (Wear Some Flowers in Your Hair)Scott McKenzie
1981Hold On TightElectric Light Orchestra
1987Lost In EmotionLisa Lisa & Cult Jam
2005Best of YouThe Foo Fighters
1975LadyStyx
1999All StarSmash Mouth
1956I Almost Lost My MindPat Boone
1966Bang Bang (My Baby Shot Me Down)Cher
2010Waka Waka (This Time For Africa)Shakira & Freshly Ground
1990VogueMadonna
2005Blue OrchidThe White Stripes
1950Foggy Mountain BreakdownLester Flatt & Earl Scruggs
1978Baby Come BackPlayer
1959DonnaRitchie Valens
1980One Step BeyondMadness
1991JoyrideRoxette
1965The Tracks of My TearsThe Miracles
1989Free Fallin'Tom Petty & The Heartbreakers
1981Stop the CavalryJona Lewie
1971The Night They Drove Old Dixie DownJoan Baez
1951Cold, Cold HeartTony Bennett
1990Tom's DinerDNA & Suzanne Vega
1994AlwaysBon Jovi
1956Hound DogElvis Presley
1988Roll With ItSteve Winwood
1998Together AgainJanet Jackson
2004Do They Know It's Christmas?Band Aid 20
1958Twilight TimeThe Platters
1972Black DogLed Zeppelin
1998Brimful of AshaCornershop
1942Sleepy LagoonHarry James
2001Here With MeDido
1982AbracadabraSteve Miller Band
2004SuperstarJamelia
1973You're So VainCarly Simon
1992The OneElton John
1975SOSAbba
1997I Don't Want ToToni Braxton
1956Poor People of ParisLes Baxter & his Orchestra
1989PoisonAlice Cooper
1969It's Your ThingThe Isley Brothers
1996Because You Loved MeCeline Dion
1992Can't Let GoMariah Carey
1966You Don't Have to Say You Love MeDusty Springfield
1923Down Hearted BluesBessie Smith
2007ParalyzerFinger Eleven
1976Don't Go Breaking My HeartElton John & Kiki Dee
2013Blurred LinesRobin Thicke, T.I. & Pharrell Williams
1989If I Could Turn Back TimeCher
2003Not Gonna Get Ust.A.T.u.
2004Through the WireKanye West
2010We No Speak AmericanoYolanda Be Cool & DCup
1986Hit That Perfect BeatBronski Beat
1969Ob-La-Di, Ob-La-DaThe Beatles
2010All The Right MovesOneRepublic
1996MacarenaLos Del Rio
1983Union of the SnakeDuran Duran
1975I Can't Give You Anything (But My Love)The Stylistics
1994Mmm Mmm Mmm MmmCrash Test Dummies
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>