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
1937MarieTommy Dorsey
1981RaptureBlondie
1979Highway to HellAC/DC
2004The ReasonHoobastank
1983Karma ChameleonCulture Club
1973Live & Let DieWings
1963From a Jack to a KingNed Miller
2003So YesterdayHilary Duff
1984To All the Girls I've Loved BeforeJulio Iglesias & Willie Nelson
1954Sh-Boom (Life Could Be a Dream)Chords
1968The Good the Bad & the UglyHugo Montenegro
2010We No Speak AmericanoYolanda Be Cool & DCup
2004Left Outside AloneAnastacia
1955Rock Around the ClockBill Haley & his Comets
2008No AirJordin Sparks & Chris Brown
1995Anywhere IsEnya
1965(I Can't Get No) SatisfactionThe Rolling Stones
1990U Can't Touch ThisMC Hammer
1984Love of the Common PeoplePaul Young
1909Swing Low, Sweet ChariotFisk University Jubilee Quartet
1967Let's Spend the Night TogetherThe Rolling Stones
1972Where is the Love?Roberta Flack & Donny Hathaway
2010What Do You Want From Me?Adam Lambert
1957Jailhouse RockElvis Presley
1966The Sun Ain't Gonna Shine (Anymore)The Walker Brothers
1968Legend of XanaduDave Dee, Dozy, Beaky, Mick & Tich
1949You're Breakin' My HeartThe Ink Spots
2002EscapeEnrique Iglesias
1959Put Your Head On My ShoulderPaul Anka
2001Ride Wit MeNelly
2001Moi... LolitaAlizee
2006Don't Forget About UsMariah Carey
1957Wonderful WonderfulJohnny Mathis
1979We Don't Talk AnymoreCliff Richard
1971Reason to BelieveRod Stewart
1988Love Changes EverythingClimie Fisher
1962Wolverton MountainClaude King
2002PerdonoTiziano Ferro
1982The Lion Sleeps TonightTight Fit
1963Can't Get Used to Losing YouAndy Williams
1970Get ReadyRare Earth
1913When Irish Eyes Are SmilingChauncy Olcott
1991DisappearINXS
1988The Only Way is UpYazz & The Plastic Population
1921Second Hand RoseFanny Brice
1986Two Of HeartsStacey Q
1965Like a Rolling StoneBob Dylan
1974Lucy in the Sky With DiamondsElton John
2004Behind Blue EyesLimp Bizkit
1980SaraFleetwood Mac
1971Your SongElton John
1924CharlestonArthur Gibbs & his Gang
1935Silent Night, Holy NightBing Crosby
1986Stuck With YouHuey Lewis & The News
1966We Can Work it OutThe Beatles
2002Like I Love YouJustin Timberlake
2003Stand UpLudacris & Shaunna
1962Happy Birthday Sweet SixteenNeil Sedaka
1973Hi Hi HiWings
1930Three Little WordsDuke Ellington
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>