Here is a simple example of displaying MySQL data in a Bootstrap table:
Year | Song Title | Artist |
---|---|---|
1965 | It's Not Unusual | Tom Jones |
1978 | Flash Light | Parliament |
1940 | New San Antonio Rose | Bob Wills & his Texas Playboys |
2011 | Mr Saxobeat | Alexandra Stan |
1973 | Smoke On the Water | Deep Purple |
1969 | Jean | Oliver |
1958 | Bird Dog | The Everly Brothers |
1978 | Mary's Boy Child/Oh My Lord | Boney M |
1988 | How Can I Fall | Breathe |
1994 | Basket Case | Green Day |
1987 | Breakout | Swing Out Sister |
1961 | Spanish Harlem | Ben E King |
1954 | Make Love to Me | Jo Stafford |
1966 | Yesterday Man | Chris Andrews |
1964 | Hippy Hippy Shake | Swinging Blue Jeans |
1954 | Hernando's Hideaway | Archie Bleyer |
1990 | Get Up (Before the Night is Over) | Technotronic |
1959 | Donna | Ritchie Valens |
2007 | Sweetest Girl (dollar Bill) | Wyclef Jean & Akon & Lil' Wayne |
1997 | For You I Will | Monica |
2004 | Lose My Breath | Destiny's Child |
1951 | Dust My Broom | Elmore James |
1950 | If I Knew You Were Comin' I'd Have Baked a Cake | Eve Young |
2004 | Goodies | Ciara & Petey Pablo |
2007 | Mr Bartender | T-Pain |
1999 | Baby One More Time | Britney Spears |
2000 | Go Let it Out | Oasis |
1994 | Mmm Mmm Mmm Mmm | Crash Test Dummies |
1946 | Ole Buttermilk Sky | Kay Kyser |
1958 | Do You Want to Dance? | Bobby Freeman |
1969 | Bad Moon Rising | Creedence Clearwater Revival |
1974 | Eres Tu (Touch The Wind) | Mocedades |
1970 | All Right Now | Free |
1996 | Nobody Knows | Tony Rich Project |
1976 | Love Really Hurts Without You | Billy Ocean |
1998 | Ray of Light | Madonna |
2009 | Gives You Hell | All-American Rejects |
1966 | 96 Tears | Question Mark & The Mysterians |
1990 | Downtown Train | Rod Stewart |
1986 | Higher Love | Steve Winwood |
2005 | Stickwitu | The Pussycat Dolls |
2012 | We Are Young | fun. |
2010 | Airplanes | BoB & Hayley Williams |
2003 | 21 Questions | 50 Cent |
2004 | Dry Your Eyes | The Streets |
2010 | Raise Your Glass | Pink |
1955 | Tutti Frutti | Little Richard |
1968 | World | Bee Gees |
1952 | Botch-a-me (Ba-ba-baciami piccina) | Rosemary Clooney |
1968 | Lady Willpower | Gary Puckett & The Union Gap |
2000 | Music | Madonna |
1985 | Alive & Kicking | Simple Minds |
1959 | El Paso | Marty Robbins |
1980 | Do That to Me One More Time | Captain & Tennille |
1965 | I'm Henry the Eighth I Am | Herman's Hermits |
1985 | You're the Inspiration | Chicago |
1987 | Caravan of Love | The Housemartins |
2005 | Lonely No More | Rob Thomas |
1995 | Let Her Cry | Hootie & The Blowfish |
2001 | Somethin' Stupid | Robbie Williams & Nicole Kidman |
<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
$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();
?>
If you want the table to be responsive, meaning it will adapt to smaller screens, wrap the <table>
inside a .table-responsive
class:
<div class="table-responsive">
<table class="table table-striped">
<!-- Table content goes here -->
</table>
</div>
<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>