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 |
---|---|---|
1934 | June in January | Bing Crosby |
1989 | Listen to Your Heart | Roxette |
1966 | Yesterday Man | Chris Andrews |
1986 | The Next Time I Fall | Peter Cetera & Amy Grant |
1972 | Black & White | Three Dog Night |
2006 | Over My Head (Cable Car) | The Fray |
1987 | In Too Deep | Genesis |
1984 | Miss Me Blind | Culture Club |
1988 | Alphabet Street | Prince |
1951 | Longing For You | Teresa Brewer |
1981 | Morning Train (Nine to Five) | Sheena Easton |
1977 | You Make Loving Fun | Fleetwood Mac |
1985 | Kayleigh | Marillion |
1995 | Scatman's World | Scatman John |
1973 | Angie | The Rolling Stones |
2005 | Don't Phunk With My Heart | The Black Eyed Peas |
2010 | Cooler than me | Mike Posner |
1999 | Back At One | Brian McKnight |
2005 | Hung Up | Madonna |
1959 | Mack the Knife | Bobby Darin |
1967 | Jackson | Nancy Sinatra & Lee Hazlewood |
1966 | I am a Rock | Simon & Garfunkel |
1909 | Swing Low, Sweet Chariot | Fisk University Jubilee Quartet |
1973 | Drift Away | Dobie Gray |
1944 | Is You Is or Is You Ain't (Ma' Baby) | Louis Jordan |
1989 | The Look | Roxette |
2001 | Jaded | Aerosmith |
1977 | Dreams | Fleetwood Mac |
1952 | You Belong to Me | Jo Stafford |
1978 | Wonderful Tonight | Eric Clapton |
1982 | Eye in the Sky | The Alan Parsons Project |
2005 | Trippin' | Robbie Williams |
1979 | Bad Case of Loving You | Robert Palmer |
1992 | Rump Shaker | Wreckx-N-Effect |
2002 | Hands Clean | Alanis Morissette |
1975 | Una Paloma Blanca | George Baker Selection |
1975 | Lady Marmalade (Voulez-Vous Coucher Aver Moi Ce Soir?) | Patti LaBelle |
1986 | Spirit in the Sky | Doctor & The Medics |
1989 | Born to Be My Baby | Bon Jovi |
1965 | Over & Over | Dave Clark Five |
1984 | Let's Hear it For the Boy | Deniece Williams |
2003 | Rock Wit U (Awww Baby) | Ashanti |
1954 | Cara Mia | David Whitfield & Mantovani |
1981 | Antmusic | Adam & The Ants |
1991 | Change | Lisa Stansfield |
1956 | True Love | Bing Crosby & Grace Kelly |
1990 | Thieves in the Temple | Prince |
2000 | Could I Have This Kiss Forever | Whitney Houston & Enrique Iglesias |
1978 | Dreadlock Holiday | 10CC |
1996 | Name | The Goo Goo Dolls |
1984 | Footloose | Kenny Loggins |
1977 | God Save the Queen | The Sex Pistols |
2006 | Me & U | Cassie |
1988 | Foolish Beat | Debbie Gibson |
1993 | Hero | Mariah Carey |
1987 | Boys (Summertime Love) | Sabrina |
1988 | I Will Always Love You | Taylor Dayne |
2002 | Perdono | Tiziano Ferro |
1991 | Wicked Game | Chris Isaak |
1964 | Remember (Walkin' in the Sand) | The Shangri-Las |
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