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
1998BelieveCher
1993Another Sad Love SongToni Braxton
1972Black & WhiteThree Dog Night
2003TROUBLEPink
1985Axel FHarold Faltermeyer
1989Rock OnMichael Damian
1977When I Need YouLeo Sayer
1983Love is a BattlefieldPat Benatar
1953I BelieveFrankie Laine
1988PerfectFairground Attraction
1982I've Never Been to MeCharlene
1998Music Sounds Better With YouStardust
1975Sister Golden HairAmerica
2003Are You Happy NowMichelle Branch
1966Little ManSonny & Cher
2004BurnUsher
1962Dream Baby (How Long Must I Dream)Roy Orbison
2009Evacuate The DancefloorCascada
1964Non Ho L'Eta Per AmartiGigliola Cinquetti
1959Since I Don't Have YouSkyliners
1943You'll Never KnowDick Haymes
2002The Tide is High (Get the Feeling)Atomic Kitten
1995Roll With ItOasis
1996You LearnAlanis Morissette
1992Get Ready For This2 Unlimited
2005Let Me Love YouMario
2001Because I Got HighAfroman
1965We Gotta Get Out of This PlaceThe Animals
1944San Fernando ValleyBing Crosby
2009The ClimbMiley Cyrus
1980FashionDavid Bowie
1952There's Always Room At Our HouseGuy Mitchell
2009Empire State Of MindJay-Z & Alicia Keys
1960My Old Man's a Dustman (Ballad of a Refuse Disposal Officer)Lonnie Donegan
1973Smoke On the WaterDeep Purple
1922I'll Build a Stairway to ParadisePaul Whiteman
1945Till The End of TimePerry Como
1960Teen AngelMark Dinning
1995Don't Take It Personal (Just One Of Dem Days)Monica
1978EmotionSamantha Sang
1969Lily the PinkThe Scaffold
1997In My BedDru Hill
1956Poor People of ParisLes Baxter & his Orchestra
1928West End BluesLouis Armstrong
1972Daddy Don't You Walk So FastWayne Newton
1972Tumbling DiceThe Rolling Stones
1961Little SisterElvis Presley
1978Boogie Oogie OogieA Taste of Honey
1991Rush RushPaula Abdul
2006Far AwayNickelback
1965A Taste of HoneyHerb Alpert
1970Ball of Confusion (That's What the World is Today)The Temptations
1942Blues in the NightWoody Herman
2002Electrical StormU2
1979When You're in Love With a Beautiful WomanDr Hook
1946I'm a Big Girl NowSammy Kaye
1990All Or NothingMilli Vanilli
1976Lonely Night (Angel Face)Captain & Tennille
2001Only TimeEnya
1991UnbelievableEMF
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>