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 |
---|---|---|
1992 | One | U2 |
2010 | Firework | Katy Perry |
1969 | Little Woman | Bobby Sherman |
1994 | Confide in Me | Kylie Minogue |
1990 | U Can't Touch This | MC Hammer |
1979 | I Will Survive | Gloria Gaynor |
1966 | We Can Work it Out | The Beatles |
1960 | Cradle of Love | Johnny Preston |
1956 | Moonglow & Theme From Picnic | Morris Stoloff |
2000 | Kryptonite | Three Doors Down |
2010 | Barbra Streisand | Duck Sauce |
2007 | No One | Alicia Keys |
1957 | My Special Angel | Bobby Helms |
1982 | I've Never Been to Me | Charlene |
1984 | Joanna | Kool & The Gang |
1985 | Saving All My Love For You | Whitney Houston |
1949 | Mule Train | Tennessee Ernie Ford |
1971 | Mozart Symphony No 40 in G Minor | Waldo De Los Rios |
1974 | The Night Chicago Died | Paper Lace |
1968 | Angel of the Morning | Merrilee Rush |
1995 | Kiss From a Rose | Seal |
1999 | It's All Been Done | Barenaked Ladies |
2004 | My Band | D12 |
1992 | Am I the Same Girl | Swing Out Sister |
1966 | Sunshine Superman | Donovan |
1945 | Along the Navajo Trail | Dinah Shore |
1976 | Welcome Back | John Sebastian |
1997 | Karma Police | Radiohead |
1984 | Hold Me Now | The Thompson Twins |
1977 | Float On | The Floaters |
2007 | Fergalicious | Fergie & will.i.am |
1946 | Doctor, Lawyer, Indian Chief | Betty Hutton |
1972 | Too Late To Turn Back Now | Cornelius Brothers & Sister Rose |
2011 | The Edge Of Glory | Lady GaGa |
1974 | Spiders & Snakes | Jim Stafford |
1971 | I Am... I Said | Neil Diamond |
1925 | If You Knew Susie (Like I Know Susie) | Eddie Cantor |
1955 | Autumn Leaves (Les Feuilles Mortes) | Roger Williams |
2008 | Shake It | Metro Station |
1938 | You Must Have Been a Beautiful Baby | Bing Crosby |
1982 | The Lion Sleeps Tonight | Tight Fit |
1961 | Wheels | String-A-Longs |
1996 | Hit Me Off | New Edition |
1988 | Pink Cadillac | Natalie Cole |
1999 | No Scrubs | TLC |
1997 | Barbie Girl | Aqua |
2009 | Celebration | Madonna |
1981 | Rock This Town | The Stray Cats |
1964 | It's All Over Now | The Rolling Stones |
1955 | Cool Water | Frankie Laine |
1957 | Lucille | Little Richard |
2001 | Moi... Lolita | Alizee |
1972 | Look What You Done for Me | Al Green |
1979 | Knock On Wood | Amii Stewart |
1957 | Young Love | Tab Hunter |
1948 | Powder Your Face With Sunshine | Evelyn Knight & The Stardusters |
1957 | Wake Up Little Susie | The Everly Brothers |
1996 | Words | Boyzone |
2005 | Beautiful Soul | Jesse McCartney |
1968 | Cinderella Rockefella | Esther & Abi Ofarim |
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