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 |
---|---|---|
1969 | Spinning Wheel | Blood Sweat & Tears |
1967 | San Francisco (Wear Some Flowers in Your Hair) | Scott McKenzie |
1981 | Hold On Tight | Electric Light Orchestra |
1987 | Lost In Emotion | Lisa Lisa & Cult Jam |
2005 | Best of You | The Foo Fighters |
1975 | Lady | Styx |
1999 | All Star | Smash Mouth |
1956 | I Almost Lost My Mind | Pat Boone |
1966 | Bang Bang (My Baby Shot Me Down) | Cher |
2010 | Waka Waka (This Time For Africa) | Shakira & Freshly Ground |
1990 | Vogue | Madonna |
2005 | Blue Orchid | The White Stripes |
1950 | Foggy Mountain Breakdown | Lester Flatt & Earl Scruggs |
1978 | Baby Come Back | Player |
1959 | Donna | Ritchie Valens |
1980 | One Step Beyond | Madness |
1991 | Joyride | Roxette |
1965 | The Tracks of My Tears | The Miracles |
1989 | Free Fallin' | Tom Petty & The Heartbreakers |
1981 | Stop the Cavalry | Jona Lewie |
1971 | The Night They Drove Old Dixie Down | Joan Baez |
1951 | Cold, Cold Heart | Tony Bennett |
1990 | Tom's Diner | DNA & Suzanne Vega |
1994 | Always | Bon Jovi |
1956 | Hound Dog | Elvis Presley |
1988 | Roll With It | Steve Winwood |
1998 | Together Again | Janet Jackson |
2004 | Do They Know It's Christmas? | Band Aid 20 |
1958 | Twilight Time | The Platters |
1972 | Black Dog | Led Zeppelin |
1998 | Brimful of Asha | Cornershop |
1942 | Sleepy Lagoon | Harry James |
2001 | Here With Me | Dido |
1982 | Abracadabra | Steve Miller Band |
2004 | Superstar | Jamelia |
1973 | You're So Vain | Carly Simon |
1992 | The One | Elton John |
1975 | SOS | Abba |
1997 | I Don't Want To | Toni Braxton |
1956 | Poor People of Paris | Les Baxter & his Orchestra |
1989 | Poison | Alice Cooper |
1969 | It's Your Thing | The Isley Brothers |
1996 | Because You Loved Me | Celine Dion |
1992 | Can't Let Go | Mariah Carey |
1966 | You Don't Have to Say You Love Me | Dusty Springfield |
1923 | Down Hearted Blues | Bessie Smith |
2007 | Paralyzer | Finger Eleven |
1976 | Don't Go Breaking My Heart | Elton John & Kiki Dee |
2013 | Blurred Lines | Robin Thicke, T.I. & Pharrell Williams |
1989 | If I Could Turn Back Time | Cher |
2003 | Not Gonna Get Us | t.A.T.u. |
2004 | Through the Wire | Kanye West |
2010 | We No Speak Americano | Yolanda Be Cool & DCup |
1986 | Hit That Perfect Beat | Bronski Beat |
1969 | Ob-La-Di, Ob-La-Da | The Beatles |
2010 | All The Right Moves | OneRepublic |
1996 | Macarena | Los Del Rio |
1983 | Union of the Snake | Duran Duran |
1975 | I Can't Give You Anything (But My Love) | The Stylistics |
1994 | Mmm Mmm Mmm Mmm | Crash Test Dummies |
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