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 |
---|---|---|
1996 | Coco Jamboo | Mr President |
1981 | Slow Hand | The Pointer Sisters |
1990 | Come Back to Me | Janet Jackson |
1958 | A Certain Smile | Johnny Mathis |
1988 | Im Nin'Alu | Ofra Haza |
1998 | 9pm (Till I Come) | ATB |
1999 | If You Had My Love | Jennifer Lopez |
2004 | Just Lose It | Eminem |
1986 | All I Need is a Miracle | Mike & The Mechanics |
2010 | Waka Waka (This Time For Africa) | Shakira & Freshly Ground |
1984 | Miss Me Blind | Culture Club |
1961 | Daddy's Home | Shep & the Limelites |
1947 | That's My Desire | Frankie Laine |
1989 | Every Rose Has It's Thorn | Poison |
1969 | I'll Never Fall in Love Again | Tom Jones |
1984 | Ghostbusters | Ray Parker Jr |
1998 | My Favourite Mistake | Sheryl Crow |
1969 | (Call Me) Number One | The Tremeloes |
1967 | It Must Be Him (Seul Sur Son Etoile) | Vikki Carr |
1986 | Manic Monday | The Bangles |
1960 | Teen Angel | Mark Dinning |
1973 | Also Sprach Zarathustra (2001) | Eumir Deodato |
1990 | Killer | Adamski |
1960 | Mule Skinner Blues | Fendermen |
1976 | Theme from 'SWAT' | Rhythm Heritage |
1998 | When the Lights Go Out | Five |
1976 | Baby, I Love Your Way | Peter Frampton |
1972 | Layla | Derek & The Dominos |
1955 | Heart of Stone | The Fontane Sisters |
1996 | C'Mon Ride the Train | Quad City DJs |
1927 | Someone to Watch Over Me | Gertrude Lawrence |
1985 | St Elmo's Fire (Man in Motion) | John Parr |
1984 | Do They Know It's Christmas? | Band Aid |
1987 | Heart & Soul | T'Pau |
1973 | Nutbush City Limits | Ike & Tina Turner |
1998 | Goodbye | Spice Girls |
2003 | Lifestyles of the Rich & Famous | Good Charlotte |
1998 | From This Moment On | Shania Twain |
1965 | Wooly Bully | Sam The Sham & The Pharaohs |
1986 | Addicted to Love | Robert Palmer |
1996 | 1,2,3,4 (Sumpin' New) | Coolio |
1960 | Greenfields | Brothers Four |
1980 | Tired of Toein' The Line | Rocky Burnett |
2008 | Love Lockdown | Kanye West |
1962 | Don't Break the Heart That Loves You | Connie Francis |
1997 | Honey | Mariah Carey |
1949 | Forever & Ever | Russ Morgan |
1977 | We Are the Champions | Queen |
1966 | What Becomes of the Broken Hearted | Jimmy Ruffin |
2001 | What it Feels Like For a Girl | Madonna |
1995 | Army of Me | Bjork |
2001 | Butterfly | Crazy Town |
1985 | Money For Nothing | Dire Straits |
2004 | Jesus Walks | Kanye West |
1926 | I'm Sitting On Top of the World | Al Jolson |
2006 | I Don't Feel Like Dancin' | The Scissor Sisters |
2012 | Domino | Jessie J |
1979 | Some Girls | Racey |
1981 | Jealous Guy | Roxy Music |
1967 | Waterloo Sunset | The Kinks |
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