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
1995You Gotta BeDes'ree
1985Don't You (Forget About Me)Simple Minds
1983Baby JaneRod Stewart
1945I Can't Begin to Tell YouBing Crosby & Carmen Cavallaro
1992End of the RoadBoyz II Men
1930Puttin' on the RitzHarry Richman
2005Don't ChaThe Pussycat Dolls
1954Earth AngelThe Penguins
1998From This Moment OnShania Twain
1982Jack & DianeJohn Cougar Mellencamp
1999Larger Than LifeThe Backstreet Boys
1969You've Made Me So Very HappyBlood Sweat & Tears
1989Ride On TimeBlack Box
2001Don't Stop Movin'S Club 7
1994Don't Stop (Wiggle Wiggle)The Outhere Brothers
1967WindyAssociation
1952AnytimeEddie Fisher
1977Undercover AngelAlan O'Day
1985Take On MeA-Ha
1963Bossa Nova BabyElvis Presley
1994AmazingAerosmith
1959MorgenIvo Robic
1960Sweet Nothin'sBrenda Lee
1986Touch Me (I Want Your Body)Samantha Fox
1995A Girl Like YouEdwyn Collins
1973Skweeze Me, Pleeze MeSlade
1998Music Sounds Better With YouStardust
1985Some Like it HotPower Station
1947BallerinaVaughn Monroe
1990Lily Was HereDavid A Stewart
1958C'Mon EverybodyEddie Cochran
1992I'd Die Without YouPM Dawn
1993All That She WantsAce of Base
1981This Ole HouseShakin' Stevens
1955Ballad of Davy CrockettFess Parker
1961Are You Sure?The Allisons
1960The Wild OneBobby Rydell
1942I Don't Want to Walk Without YouHarry James
1975Fly Robin FlySilver Convention
1992It's My LifeDr Alban
1971WoodstockMatthews Southern Comfort
1979Just When I Needed You MostRandy Vanwarmer
1973Gudbye T'JaneSlade
2005Candy Shop50 Cent & Olivia
1964Memphis, TennesseeJohnny Rivers
2013RoarKaty Perry
2004Slow JamzTwista
1978You Needed MeAnne Murray
1942Who Wouldn't Love You?Kay Kyser
2002GirlfriendN Sync
1991CreamPrince
2002Just a LittleLiberty X
1947Open The Door, Richard!Count Basie
19989pm (Till I Come)ATB
2011S&MRihanna
2004Pieces of MeAshlee Simpson
2003Sorry Seems to Be the Hardest WordBlue & Elton John
1995I KnowDionne Farris
1962Peppermint TwistJoey Dee & The Starliters
1980Call MeBlondie
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>