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
1993The River of DreamsBilly Joel
1978Copacabana (At the Copa)Barry Manilow
1972Troglodyte (Cave Man)Jimmy Castor Bunch
1966It's a Man's Man's Man's WorldJames Brown
2007Cupid's chokehold (Breakfast in America)Gym Class Heroes
1991I Adore Mi AmorColor Me Badd
1985This is Not AmericaDavid Bowie & The Pat Metheny Group
1959The Three Bells (Les Trois Cloches)The Browns
1990FreedomGeorge Michael
1976Lady BumpPenny McLean
2005OhCiara & Ludacris
1965My GirlThe Temptations
1977Gonna Fly Now (Theme From 'Rocky')Bill Conti
1957Banana Boat SongHarry Belafonte
2003I Believe in a Thing Called LoveThe Darkness
1989After AllPeter Cetera & Cher
1945Caldonia Boogie (What Makes Your Big Head So Hard)Louis Jordan
1960Save the Last Dance For MeThe Drifters
2003Harder to BreatheMaroon 5
1967Carrie-AnneThe Hollies
2002Tainted LoveMarilyn Manson
2007Relax, Take It EasyMika
1985I Want to Know What Love IsForeigner
2007BrianstormArctic Monkeys
1984Self ControlLaura Branigan
1939If I Didn't CareThe Ink Spots
1981How 'bout UsChampaign
2000I Wanna Love You ForeverJessica Simpson
2000Don't Tell MeMadonna
1976Love HangoverDiana Ross
1976Right Back Where We Started FromMaxine Nightingale
1950I Can Dream, Can't I?The Andrews Sisters
1972I am WomanHelen Reddy
1987Wishing WellTerence Trent D'Arby
1989The BestTina Turner
2000American PieMadonna
1960North to AlaskaJohnny Horton
1965I'll Never Find Another YouThe Seekers
2006JumpMadonna
1988Father FigureGeorge Michael
2007Hate That I Love YouRihanna
2006CrazyGnarls Barkley
1977L'oiseau Et L'enfantMarie Myriam
1982Down UnderMen At Work
1949Careless HandsMel Torme
1999Where My Girls At?702
1999Wish I Could FlyRoxette
1998Hard Knock LifeJay-Z
1966Rainy Day Woman Nos 12 & 35Bob Dylan
1963Lucky LipsCliff Richard
1974Smokin' in the Boys' RoomBrownsville Station
2003Big Yellow TaxiCounting Crows
2009Whatcha SayJason Derulo
1959Lipstick On Your CollarConnie Francis
2002A Thousand MilesVanessa Carlton
1995Conquest of ParadiseVangelis
1994I Like to Move ItReel 2 Real
1990Still Got the Blues (For You)Gary Moore
1989So AliveLove & Rockets
1975Please Mr PostmanThe Carpenters
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>