Here is a simple example of displaying MySQL data in a Bootstrap table:
Year | Song Title | Artist |
---|---|---|
1971 | Go Away Little Girl | Donny Osmond |
1971 | I Beg Your Pardon (I Never Promised You a Rose Garden) | Lynn Anderson |
1979 | One Way Ticket | Eruption |
1984 | Sounds Like a Melody | Alphaville |
2003 | Anyone of Us (Stupid Mistake) | Gareth Gates |
1950 | The Thing | Phil Harris |
1974 | Devil Gate Drive | Suzi Quatro |
1995 | You Oughta Know | Alanis Morissette |
1976 | The Rubberband Man | The Detroit Spinners |
1998 | Walkin' On the Sun | Smash Mouth |
2001 | Smooth Criminal | Alien Ant Farm |
2006 | Waiting on the World to Change | John Mayer |
1977 | Mull of Kintyre | Wings |
1980 | The Second Time Around | Shalamar |
1979 | I Was Made For Lovin' You | Kiss |
1991 | Love is a Wonderful Thing | Michael Bolton |
2006 | Suddenly I See | KT Tunstall |
2003 | Ignition | R Kelly |
1986 | There'll Be Sad Songs (To Make You Cry) | Billy Ocean |
1983 | Maniac | Michael Sembello |
1980 | The Rose | Bette Midler |
1920 | Swanee | Al Jolson |
2008 | Live Your Life | T.I. & Rihanna |
1971 | She's a Lady | Tom Jones |
1985 | Just Another Night | Mick Jagger |
2005 | Incomplete | The Backstreet Boys |
1985 | Material Girl | Madonna |
1986 | Living Doll | Cliff Richard & The Young Ones |
1982 | The Land of Make Believe | Bucks Fizz |
1989 | I Don't Wanna Lose You | Gloria Estefan |
1974 | Tiger Feet | Mud |
1994 | Always | Bon Jovi |
1941 | Stardust | Artie Shaw |
1999 | That Don't Impress Me Much | Shania Twain |
2005 | Cool | Gwen Stefani |
1989 | Love Train | Holly Johnson |
1988 | I Think We're Alone Now | Tiffany |
2001 | In the End | Linkin Park |
1949 | That Lucky Old Sun (Just Rolls Around Heaven All Day) | Frankie Laine |
1993 | I Have Nothing | Whitney Houston |
1972 | Son of My Father | Chicory Tip |
1927 | Me & My Shadow | Jack Smith |
1968 | White Room | Cream |
1979 | I Got My Mind Made Up | Instant Funk |
1979 | We Are Family | Sister Sledge |
2006 | Pump It | The Black Eyed Peas |
1964 | Under the Boardwalk | The Drifters |
1998 | Intergalactic | The Beastie Boys |
1974 | Living For the City | Stevie Wonder |
1961 | Little Sister | Elvis Presley |
1994 | Regulate | Warren G & Nate Dogg |
2006 | The Saints are Coming | U2 & Green Day |
2004 | Jesus Walks | Kanye West |
1929 | Honey | Rudy Vallee & his Connecticut Yankees |
1993 | Mr Vain | Culture Beat |
1985 | The Power of Love | Huey Lewis & The News |
1971 | Double Barrel | Dave & Ansil Collins |
1958 | Stupid Cupid | Connie Francis |
2009 | I Know You Want Me (calle Ocho) | Pitbull |
1959 | Roulette | Russ Conway |
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