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 |
|---|---|---|
| 1971 | Smiling Faces Sometimes | The Undisputed Truth |
| 1997 | Stand By Me | Oasis |
| 1997 | Where's the Love | Hanson |
| 1974 | I Honestly Love You | Olivia Newton-John |
| 2009 | Party In The U.s.a. | Miley Cyrus |
| 1967 | Pleasant Valley Sunday | The Monkees |
| 2004 | What U Waitin' 4 | Gwen Stefani |
| 2009 | Russian Roulette | Rihanna |
| 1974 | Hooked On a Feeling | Blue Swede |
| 2000 | Everything You Want | Vertical Horizon |
| 1988 | Love Changes Everything | Climie Fisher |
| 1965 | Il Silenzio | Nini Rosso |
| 1999 | Back At One | Brian McKnight |
| 1983 | Wrapped Around Your Finger | The Police |
| 1942 | Deep in the Heart of Texas | Alveno Rey |
| 2004 | Everything | Alanis Morissette |
| 1977 | I'm in You | Peter Frampton |
| 1962 | Good Luck Charm | Elvis Presley |
| 1997 | Can't Nobody Hold Me Down | P Diddy & Mase |
| 2003 | Stand Up | Ludacris & Shaunna |
| 1995 | Candy Rain | Soul For Real |
| 2005 | Rich Girl | Gwen Stefani |
| 1983 | Jeopardy | Greg Kihn Band |
| 1920 | Crazy Blues | Mamie Smith |
| 2013 | Blurred Lines | Robin Thicke, T.I. & Pharrell Williams |
| 2010 | Dynamite | Taio Cruz |
| 1984 | Original Sin | INXS |
| 2013 | Can't Hold Us | Macklemore & Ryan Lewis |
| 2000 | Could I Have This Kiss Forever | Whitney Houston & Enrique Iglesias |
| 2013 | Stay | Rihanna & Mikky Ekko |
| 1982 | The Look of Love | ABC |
| 1969 | And When I Die | Blood Sweat & Tears |
| 2007 | Kiss Kiss | Chris Brown & T-Pain |
| 2007 | Valerie | Mark Ronson & Amy Winehouse |
| 1989 | Eternal Flame | The Bangles |
| 2005 | Don't Lie | The Black Eyed Peas |
| 1965 | The Last Time | The Rolling Stones |
| 1962 | I Can't Stop Loving You | Ray Charles |
| 1991 | Shiny Happy People | REM |
| 1967 | (The Lights Went Out In) Massachusetts | Bee Gees |
| 1972 | Long Haired Lover From Liverpool | Little Jimmy Osmond |
| 1999 | Man! I Feel Like a Woman! | Shania Twain |
| 1949 | Cruising Down the River | Russ Morgan |
| 1984 | Missing You | John Waite |
| 1963 | I Will Follow Him | Little Peggy March |
| 1974 | The Entertainer | Marvin Hamlisch |
| 1981 | Our Lips Are Sealed | The Go Gos |
| 1994 | Anytime You Need a Friend | Mariah Carey |
| 1967 | White Rabbit | Jefferson Airplane |
| 2004 | I Believe in You | Kylie Minogue |
| 1980 | Stomp! | Brothers Johnson |
| 1997 | 2 Become 1 | Spice Girls |
| 1964 | Oh, Pretty Woman | Roy Orbison |
| 1970 | Black Magic Woman | Santana |
| 2004 | Goodies | Ciara & Petey Pablo |
| 2002 | Dreamer | Ozzy Osbourne |
| 1987 | Wishing Well | Terence Trent D'Arby |
| 1976 | (Shake, Shake, Shake) Shake Your Booty | KC & The Sunshine Band |
| 1969 | Love Me Tonight | Tom Jones |
| 1956 | I Almost Lost My Mind | Pat Boone |
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