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 |
---|---|---|
1985 | Everything She Wants | Wham! |
1970 | Up Around the Bend | Creedence Clearwater Revival |
1983 | Red Red Wine | UB40 |
2004 | Move Ya Body | Nina Sky |
1969 | Boom Bang-A-Bang | Lulu |
1980 | Sexy Eyes | Dr Hook |
1994 | The Real Thing | 2 Unlimited |
1971 | Immigrant Song | Led Zeppelin |
1996 | Tha Crossroads | Bone Thugs-n-Harmony |
1994 | Bump N' Grind | R Kelly |
1963 | Little Town Flirt | Del Shannon |
1963 | Maria Elena | Los Indios Tabajaras |
1970 | Cracklin' Rosie | Neil Diamond |
2011 | Mr Saxobeat | Alexandra Stan |
1956 | True Love | Bing Crosby & Grace Kelly |
1985 | Rhythm of the Night | DeBarge |
2012 | Take Care | Drake & Rihanna |
1985 | You're My Heart You're My Soul | Modern Talking |
2002 | Dirrty | Christina Aguilera & Redman |
1944 | Shoo-Shoo Baby | The Andrews Sisters |
1989 | I'll Be There For You | Bon Jovi |
1955 | Softly Softly | Ruby Murray |
1966 | Ain't Too Proud to Beg | The Temptations |
1926 | Valencia | Paul Whiteman |
2003 | Chihuahua | DJ BoBo |
2010 | Take It Off | Ke$ha |
1960 | Walk Don't Run | The Ventures |
1955 | Bo Diddley | Bo Diddley |
1977 | Gonna Fly Now (Theme From 'Rocky') | Bill Conti |
1941 | I Don't Want to Set the World On Fire | Horace Heidt |
1966 | Monday Monday | The Mamas & The Papas |
1964 | I Saw Her Standing There | The Beatles |
2008 | Just Dance | Lady GaGa & Colby O'Donis |
1997 | Discotheque | U2 |
1975 | Love is the Drug | Roxy Music |
1983 | Reet Petite | Jackie Wilson |
1979 | Goodnight Tonight | Wings |
1963 | Surfin' USA | The Beach Boys |
2004 | I'm Still in Love With You | Sean Paul |
1966 | No Milk Today | Herman's Hermits |
1983 | Islands in the Stream | Kenny Rogers & Dolly Parton |
2007 | Do You Know (The Ping Pong Song) | Enrique Iglesias |
1976 | 50 Ways to Leave Your Lover | Paul Simon |
1988 | The Way You Make Me Feel | Michael Jackson |
1958 | Summertime Blues | Eddie Cochran |
1982 | Come On Eileen | Dexys Midnight Runners |
1934 | Love in Bloom | Bing Crosby |
1992 | If You Asked Me To | Celine Dion |
1964 | Ain't That Lovin' You Baby | Elvis Presley |
1976 | More Than a Feeling | Boston |
1957 | Wake Up Little Susie | The Everly Brothers |
1991 | Let's Talk About Sex | Salt-N-Pepa |
1967 | Hey Joe | Jimi Hendrix |
2002 | Stop Crying Your Heart Out | Oasis |
1981 | Urgent | Foreigner |
1996 | Virtual Insanity | Jamiroquai |
1991 | Black Or White | Michael Jackson |
1977 | Strawberry Letter 23 | Brothers Johnson |
1978 | Fool (If You Think It's Over) | Chris Rea |
2004 | Sick & Tired | Anastacia |
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