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 |
---|---|---|
2002 | Addictive | Truth Hurts |
1982 | A Town Called Malice | The Jam |
1978 | Whenever I Call You 'friend' | Kenny Loggins & Stevie Nicks |
1977 | Year of the Cat | Al Stewart |
1938 | I've Got a Pocketful of Dreams | Bing Crosby |
1949 | Some Enchanted Evening | Perry Como |
1989 | I Want it All | Queen |
2004 | Somebody Told Me | The Killers |
1989 | That's What I Like | Jive Bunny & The Mastermixers |
1952 | I Saw Mommy Kissing Santa Claus | Jimmy Boyd |
1976 | The Best Disco in Town | Ritchie Family |
2007 | Pretender | The Foo Fighters |
1985 | You Belong To The City | Glenn Frey |
1977 | New Kid in Town | Eagles |
1998 | All I Have to Give | The Backstreet Boys |
1961 | Mother-In-Law | Ernie K-Doe |
1977 | Don't Cry For Me Argentina | Julie Covington |
1980 | Another Brick in the Wall (part 2) | Pink Floyd |
1997 | Quit Playing Games (With My Heart) | The Backstreet Boys |
1941 | Green Eyes | Jimmy Dorsey |
1965 | Help Me, Rhonda | The Beach Boys |
2004 | Milkshake | Kelis |
2002 | A Thousand Miles | Vanessa Carlton |
1972 | Precious & Few | Climax |
1991 | The Shoop Shoop Song (It's in His Kiss) | Cher |
1993 | Jump They Say | David Bowie |
1984 | Against All Odds (Take a Look At Me Now) | Phil Collins |
2004 | Trick Me | Kelis |
1981 | Chequered Love | Kim Wilde |
1972 | Metal Guru | T Rex |
1971 | Soley, Soley | Middle of The Road |
1954 | Sh-Boom (Life Could Be a Dream) | The Crew-Cuts |
1953 | Tell Me a Story | Jimmy Boyd & Frankie Laine |
2001 | Emotion | Destiny's Child |
1986 | True Blue | Madonna |
2009 | Party In The U.s.a. | Miley Cyrus |
2000 | Pure Shores | All Saints |
1941 | Stardust | Artie Shaw |
1954 | Shake, Rattle & Roll | Bill Haley & his Comets |
1972 | I Gotcha | Joe Tex |
1987 | Voyage Voyage | Desireless |
1997 | Men in Black | Will Smith |
1979 | I Don't Like Mondays | The Boomtown Rats |
1994 | Rhythm of the Night | Corona |
1923 | Parade of the Wooden Soldiers | Paul Whiteman |
1973 | Frankenstein | Edgar Winter Group |
1962 | Junge komm bald wieder | Freddy Quinn |
1980 | One Step Beyond | Madness |
2009 | The Climb | Miley Cyrus |
1963 | I'm Leaving it (All) Up to You | Dale & Grace |
1976 | Dream On | Aerosmith |
1983 | Last Night a DJ Saved My Life | Indeep |
2010 | Replay | Iyaz |
1986 | Party All The Time | Eddie Murphy |
1988 | Stand Up For Your Love Rights | Yazz & The Plastic Population |
1993 | Oh Carolina | Shaggy |
1983 | Love is a Stranger | The Eurythmics |
1958 | Lollipop | The Chordettes |
1978 | Just the Way You Are | Billy Joel |
2006 | Irreplaceable | Beyonce |
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