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 |
---|---|---|
1976 | Devil Woman | Cliff Richard |
1942 | Blues in the Night | Woody Herman |
2000 | Maria Maria | Santana |
1934 | Cocktails For Two | Duke Ellington |
1992 | I'll Be There | Mariah Carey |
1995 | Be My Lover | La Bouche |
2010 | Mine | Taylor Swift |
1998 | Sex & Candy | Marcy Playground |
1968 | On the Road Again | Canned Heat |
1981 | Theme From 'Greatest American Hero' (Believe It Or Not) | Joey Scarbury |
1981 | Prince Charming | Adam & The Ants |
1979 | Reunited | Peaches & Herb |
2002 | Gimme the Light | Sean Paul |
1936 | Goody Goody | Benny Goodman |
1982 | Eye of the Tiger | Survivor |
1982 | Heart Attack | Olivia Newton-John |
1923 | Yes! We Have No Bananas | Billy Jones |
2001 | Fallin' | Alicia Keys |
1962 | Crying in the Rain | The Everly Brothers |
1993 | The River of Dreams | Billy Joel |
1956 | See You Later Alligator | Bill Haley & his Comets |
1966 | Stop Stop Stop | The Hollies |
1985 | Walking On Sunshine | Katrina & The Waves |
1970 | Don't Cry Daddy | Elvis Presley |
1949 | Cruising Down the River | Russ Morgan |
1965 | The Birds & the Bees | Jewel Akens |
1971 | Stairway to Heaven | Led Zeppelin |
1991 | Should I Stay Or Should I Go | The Clash |
2000 | 7 Days | Craig David |
1920 | Swanee | Al Jolson |
1969 | Little Woman | Bobby Sherman |
1978 | Mary's Boy Child/Oh My Lord | Boney M |
2006 | Me & U | Cassie |
1988 | Doctorin' the Tardis | KLF |
1984 | Stuck On You | Lionel Richie |
2001 | I Hope You Dance | Lee Ann Womack |
2002 | I'm Gonna Getcha Good! | Shania Twain |
1995 | Hand in My Pocket | Alanis Morissette |
1994 | Breathe Again | Toni Braxton |
2002 | Get the Party Started | Pink |
1954 | Cara Mia | David Whitfield & Mantovani |
1954 | The Little Shoemaker | The Gaylords |
1975 | Low Rider | War |
2001 | Drops of Jupiter (Tell Me) | Train |
1942 | White Christmas | Bing Crosby |
1997 | Anybody Seen My Baby | The Rolling Stones |
1972 | Listen to the Music | The Doobie Brothers |
1993 | Give it Up | Cut 'N' Move |
1986 | I Miss You | Klymaxx |
1994 | I'll Stand By You | The Pretenders |
1984 | Caribbean Queen (No More Love On the Run) | Billy Ocean |
1981 | One of Us | Abba |
1999 | Last Kiss | Pearl Jam |
2003 | Can't Hold Us Down | Christina Aguilera |
1923 | Down Hearted Blues | Bessie Smith |
1968 | Hurdy Gurdy Man | Donovan |
1983 | Making Love (Out of Nothing At All) | Air Supply |
2002 | The Zephyr Song | Red Hot Chili Peppers |
1998 | The Sweetest Thing | U2 |
1983 | Save Your Love | Renee & Renato |
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