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
2002AddictiveTruth Hurts
1982A Town Called MaliceThe Jam
1978Whenever I Call You 'friend'Kenny Loggins & Stevie Nicks
1977Year of the CatAl Stewart
1938I've Got a Pocketful of DreamsBing Crosby
1949Some Enchanted EveningPerry Como
1989I Want it AllQueen
2004Somebody Told MeThe Killers
1989That's What I LikeJive Bunny & The Mastermixers
1952I Saw Mommy Kissing Santa ClausJimmy Boyd
1976The Best Disco in TownRitchie Family
2007PretenderThe Foo Fighters
1985You Belong To The CityGlenn Frey
1977New Kid in TownEagles
1998All I Have to GiveThe Backstreet Boys
1961Mother-In-LawErnie K-Doe
1977Don't Cry For Me ArgentinaJulie Covington
1980Another Brick in the Wall (part 2)Pink Floyd
1997Quit Playing Games (With My Heart)The Backstreet Boys
1941Green EyesJimmy Dorsey
1965Help Me, RhondaThe Beach Boys
2004MilkshakeKelis
2002A Thousand MilesVanessa Carlton
1972Precious & FewClimax
1991The Shoop Shoop Song (It's in His Kiss)Cher
1993Jump They SayDavid Bowie
1984Against All Odds (Take a Look At Me Now)Phil Collins
2004Trick MeKelis
1981Chequered LoveKim Wilde
1972Metal GuruT Rex
1971Soley, SoleyMiddle of The Road
1954Sh-Boom (Life Could Be a Dream)The Crew-Cuts
1953Tell Me a StoryJimmy Boyd & Frankie Laine
2001EmotionDestiny's Child
1986True BlueMadonna
2009Party In The U.s.a.Miley Cyrus
2000Pure ShoresAll Saints
1941StardustArtie Shaw
1954Shake, Rattle & RollBill Haley & his Comets
1972I GotchaJoe Tex
1987Voyage VoyageDesireless
1997Men in BlackWill Smith
1979I Don't Like MondaysThe Boomtown Rats
1994Rhythm of the NightCorona
1923Parade of the Wooden SoldiersPaul Whiteman
1973FrankensteinEdgar Winter Group
1962Junge komm bald wiederFreddy Quinn
1980One Step BeyondMadness
2009The ClimbMiley Cyrus
1963I'm Leaving it (All) Up to YouDale & Grace
1976Dream OnAerosmith
1983Last Night a DJ Saved My LifeIndeep
2010ReplayIyaz
1986Party All The TimeEddie Murphy
1988Stand Up For Your Love RightsYazz & The Plastic Population
1993Oh CarolinaShaggy
1983Love is a StrangerThe Eurythmics
1958LollipopThe Chordettes
1978Just the Way You AreBilly Joel
2006IrreplaceableBeyonce
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>