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:
| Year | Song Title | Artist | 
|---|---|---|
| 1937 | Marie | Tommy Dorsey | 
| 1981 | Rapture | Blondie | 
| 1979 | Highway to Hell | AC/DC | 
| 2004 | The Reason | Hoobastank | 
| 1983 | Karma Chameleon | Culture Club | 
| 1973 | Live & Let Die | Wings | 
| 1963 | From a Jack to a King | Ned Miller | 
| 2003 | So Yesterday | Hilary Duff | 
| 1984 | To All the Girls I've Loved Before | Julio Iglesias & Willie Nelson | 
| 1954 | Sh-Boom (Life Could Be a Dream) | Chords | 
| 1968 | The Good the Bad & the Ugly | Hugo Montenegro | 
| 2010 | We No Speak Americano | Yolanda Be Cool & DCup | 
| 2004 | Left Outside Alone | Anastacia | 
| 1955 | Rock Around the Clock | Bill Haley & his Comets | 
| 2008 | No Air | Jordin Sparks & Chris Brown | 
| 1995 | Anywhere Is | Enya | 
| 1965 | (I Can't Get No) Satisfaction | The Rolling Stones | 
| 1990 | U Can't Touch This | MC Hammer | 
| 1984 | Love of the Common People | Paul Young | 
| 1909 | Swing Low, Sweet Chariot | Fisk University Jubilee Quartet | 
| 1967 | Let's Spend the Night Together | The Rolling Stones | 
| 1972 | Where is the Love? | Roberta Flack & Donny Hathaway | 
| 2010 | What Do You Want From Me? | Adam Lambert | 
| 1957 | Jailhouse Rock | Elvis Presley | 
| 1966 | The Sun Ain't Gonna Shine (Anymore) | The Walker Brothers | 
| 1968 | Legend of Xanadu | Dave Dee, Dozy, Beaky, Mick & Tich | 
| 1949 | You're Breakin' My Heart | The Ink Spots | 
| 2002 | Escape | Enrique Iglesias | 
| 1959 | Put Your Head On My Shoulder | Paul Anka | 
| 2001 | Ride Wit Me | Nelly | 
| 2001 | Moi... Lolita | Alizee | 
| 2006 | Don't Forget About Us | Mariah Carey | 
| 1957 | Wonderful Wonderful | Johnny Mathis | 
| 1979 | We Don't Talk Anymore | Cliff Richard | 
| 1971 | Reason to Believe | Rod Stewart | 
| 1988 | Love Changes Everything | Climie Fisher | 
| 1962 | Wolverton Mountain | Claude King | 
| 2002 | Perdono | Tiziano Ferro | 
| 1982 | The Lion Sleeps Tonight | Tight Fit | 
| 1963 | Can't Get Used to Losing You | Andy Williams | 
| 1970 | Get Ready | Rare Earth | 
| 1913 | When Irish Eyes Are Smiling | Chauncy Olcott | 
| 1991 | Disappear | INXS | 
| 1988 | The Only Way is Up | Yazz & The Plastic Population | 
| 1921 | Second Hand Rose | Fanny Brice | 
| 1986 | Two Of Hearts | Stacey Q | 
| 1965 | Like a Rolling Stone | Bob Dylan | 
| 1974 | Lucy in the Sky With Diamonds | Elton John | 
| 2004 | Behind Blue Eyes | Limp Bizkit | 
| 1980 | Sara | Fleetwood Mac | 
| 1971 | Your Song | Elton John | 
| 1924 | Charleston | Arthur Gibbs & his Gang | 
| 1935 | Silent Night, Holy Night | Bing Crosby | 
| 1986 | Stuck With You | Huey Lewis & The News | 
| 1966 | We Can Work it Out | The Beatles | 
| 2002 | Like I Love You | Justin Timberlake | 
| 2003 | Stand Up | Ludacris & Shaunna | 
| 1962 | Happy Birthday Sweet Sixteen | Neil Sedaka | 
| 1973 | Hi Hi Hi | Wings | 
| 1930 | Three Little Words | Duke 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>
Comma after each value, except the last one
Display records 
Display records in a Bootstrap Table
Display records in columns
Display records with Bootstrap pagination
Display records with CSS styles
Escape special characters in a string
Highlight the search term in results
Replace unicode characters by utf-8
Search autocomplete with Typeahead
Search records by an option  
Select rows for the current day and month
Short commands