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 |
---|---|---|
1998 | Believe | Cher |
1993 | Another Sad Love Song | Toni Braxton |
1972 | Black & White | Three Dog Night |
2003 | TROUBLE | Pink |
1985 | Axel F | Harold Faltermeyer |
1989 | Rock On | Michael Damian |
1977 | When I Need You | Leo Sayer |
1983 | Love is a Battlefield | Pat Benatar |
1953 | I Believe | Frankie Laine |
1988 | Perfect | Fairground Attraction |
1982 | I've Never Been to Me | Charlene |
1998 | Music Sounds Better With You | Stardust |
1975 | Sister Golden Hair | America |
2003 | Are You Happy Now | Michelle Branch |
1966 | Little Man | Sonny & Cher |
2004 | Burn | Usher |
1962 | Dream Baby (How Long Must I Dream) | Roy Orbison |
2009 | Evacuate The Dancefloor | Cascada |
1964 | Non Ho L'Eta Per Amarti | Gigliola Cinquetti |
1959 | Since I Don't Have You | Skyliners |
1943 | You'll Never Know | Dick Haymes |
2002 | The Tide is High (Get the Feeling) | Atomic Kitten |
1995 | Roll With It | Oasis |
1996 | You Learn | Alanis Morissette |
1992 | Get Ready For This | 2 Unlimited |
2005 | Let Me Love You | Mario |
2001 | Because I Got High | Afroman |
1965 | We Gotta Get Out of This Place | The Animals |
1944 | San Fernando Valley | Bing Crosby |
2009 | The Climb | Miley Cyrus |
1980 | Fashion | David Bowie |
1952 | There's Always Room At Our House | Guy Mitchell |
2009 | Empire State Of Mind | Jay-Z & Alicia Keys |
1960 | My Old Man's a Dustman (Ballad of a Refuse Disposal Officer) | Lonnie Donegan |
1973 | Smoke On the Water | Deep Purple |
1922 | I'll Build a Stairway to Paradise | Paul Whiteman |
1945 | Till The End of Time | Perry Como |
1960 | Teen Angel | Mark Dinning |
1995 | Don't Take It Personal (Just One Of Dem Days) | Monica |
1978 | Emotion | Samantha Sang |
1969 | Lily the Pink | The Scaffold |
1997 | In My Bed | Dru Hill |
1956 | Poor People of Paris | Les Baxter & his Orchestra |
1928 | West End Blues | Louis Armstrong |
1972 | Daddy Don't You Walk So Fast | Wayne Newton |
1972 | Tumbling Dice | The Rolling Stones |
1961 | Little Sister | Elvis Presley |
1978 | Boogie Oogie Oogie | A Taste of Honey |
1991 | Rush Rush | Paula Abdul |
2006 | Far Away | Nickelback |
1965 | A Taste of Honey | Herb Alpert |
1970 | Ball of Confusion (That's What the World is Today) | The Temptations |
1942 | Blues in the Night | Woody Herman |
2002 | Electrical Storm | U2 |
1979 | When You're in Love With a Beautiful Woman | Dr Hook |
1946 | I'm a Big Girl Now | Sammy Kaye |
1990 | All Or Nothing | Milli Vanilli |
1976 | Lonely Night (Angel Face) | Captain & Tennille |
2001 | Only Time | Enya |
1991 | Unbelievable | EMF |
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