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 |
---|---|---|
1995 | You Gotta Be | Des'ree |
1985 | Don't You (Forget About Me) | Simple Minds |
1983 | Baby Jane | Rod Stewart |
1945 | I Can't Begin to Tell You | Bing Crosby & Carmen Cavallaro |
1992 | End of the Road | Boyz II Men |
1930 | Puttin' on the Ritz | Harry Richman |
2005 | Don't Cha | The Pussycat Dolls |
1954 | Earth Angel | The Penguins |
1998 | From This Moment On | Shania Twain |
1982 | Jack & Diane | John Cougar Mellencamp |
1999 | Larger Than Life | The Backstreet Boys |
1969 | You've Made Me So Very Happy | Blood Sweat & Tears |
1989 | Ride On Time | Black Box |
2001 | Don't Stop Movin' | S Club 7 |
1994 | Don't Stop (Wiggle Wiggle) | The Outhere Brothers |
1967 | Windy | Association |
1952 | Anytime | Eddie Fisher |
1977 | Undercover Angel | Alan O'Day |
1985 | Take On Me | A-Ha |
1963 | Bossa Nova Baby | Elvis Presley |
1994 | Amazing | Aerosmith |
1959 | Morgen | Ivo Robic |
1960 | Sweet Nothin's | Brenda Lee |
1986 | Touch Me (I Want Your Body) | Samantha Fox |
1995 | A Girl Like You | Edwyn Collins |
1973 | Skweeze Me, Pleeze Me | Slade |
1998 | Music Sounds Better With You | Stardust |
1985 | Some Like it Hot | Power Station |
1947 | Ballerina | Vaughn Monroe |
1990 | Lily Was Here | David A Stewart |
1958 | C'Mon Everybody | Eddie Cochran |
1992 | I'd Die Without You | PM Dawn |
1993 | All That She Wants | Ace of Base |
1981 | This Ole House | Shakin' Stevens |
1955 | Ballad of Davy Crockett | Fess Parker |
1961 | Are You Sure? | The Allisons |
1960 | The Wild One | Bobby Rydell |
1942 | I Don't Want to Walk Without You | Harry James |
1975 | Fly Robin Fly | Silver Convention |
1992 | It's My Life | Dr Alban |
1971 | Woodstock | Matthews Southern Comfort |
1979 | Just When I Needed You Most | Randy Vanwarmer |
1973 | Gudbye T'Jane | Slade |
2005 | Candy Shop | 50 Cent & Olivia |
1964 | Memphis, Tennessee | Johnny Rivers |
2013 | Roar | Katy Perry |
2004 | Slow Jamz | Twista |
1978 | You Needed Me | Anne Murray |
1942 | Who Wouldn't Love You? | Kay Kyser |
2002 | Girlfriend | N Sync |
1991 | Cream | Prince |
2002 | Just a Little | Liberty X |
1947 | Open The Door, Richard! | Count Basie |
1998 | 9pm (Till I Come) | ATB |
2011 | S&M | Rihanna |
2004 | Pieces of Me | Ashlee Simpson |
2003 | Sorry Seems to Be the Hardest Word | Blue & Elton John |
1995 | I Know | Dionne Farris |
1962 | Peppermint Twist | Joey Dee & The Starliters |
1980 | Call Me | Blondie |
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