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 |
|---|---|---|
| 1993 | The River of Dreams | Billy Joel |
| 1978 | Copacabana (At the Copa) | Barry Manilow |
| 1972 | Troglodyte (Cave Man) | Jimmy Castor Bunch |
| 1966 | It's a Man's Man's Man's World | James Brown |
| 2007 | Cupid's chokehold (Breakfast in America) | Gym Class Heroes |
| 1991 | I Adore Mi Amor | Color Me Badd |
| 1985 | This is Not America | David Bowie & The Pat Metheny Group |
| 1959 | The Three Bells (Les Trois Cloches) | The Browns |
| 1990 | Freedom | George Michael |
| 1976 | Lady Bump | Penny McLean |
| 2005 | Oh | Ciara & Ludacris |
| 1965 | My Girl | The Temptations |
| 1977 | Gonna Fly Now (Theme From 'Rocky') | Bill Conti |
| 1957 | Banana Boat Song | Harry Belafonte |
| 2003 | I Believe in a Thing Called Love | The Darkness |
| 1989 | After All | Peter Cetera & Cher |
| 1945 | Caldonia Boogie (What Makes Your Big Head So Hard) | Louis Jordan |
| 1960 | Save the Last Dance For Me | The Drifters |
| 2003 | Harder to Breathe | Maroon 5 |
| 1967 | Carrie-Anne | The Hollies |
| 2002 | Tainted Love | Marilyn Manson |
| 2007 | Relax, Take It Easy | Mika |
| 1985 | I Want to Know What Love Is | Foreigner |
| 2007 | Brianstorm | Arctic Monkeys |
| 1984 | Self Control | Laura Branigan |
| 1939 | If I Didn't Care | The Ink Spots |
| 1981 | How 'bout Us | Champaign |
| 2000 | I Wanna Love You Forever | Jessica Simpson |
| 2000 | Don't Tell Me | Madonna |
| 1976 | Love Hangover | Diana Ross |
| 1976 | Right Back Where We Started From | Maxine Nightingale |
| 1950 | I Can Dream, Can't I? | The Andrews Sisters |
| 1972 | I am Woman | Helen Reddy |
| 1987 | Wishing Well | Terence Trent D'Arby |
| 1989 | The Best | Tina Turner |
| 2000 | American Pie | Madonna |
| 1960 | North to Alaska | Johnny Horton |
| 1965 | I'll Never Find Another You | The Seekers |
| 2006 | Jump | Madonna |
| 1988 | Father Figure | George Michael |
| 2007 | Hate That I Love You | Rihanna |
| 2006 | Crazy | Gnarls Barkley |
| 1977 | L'oiseau Et L'enfant | Marie Myriam |
| 1982 | Down Under | Men At Work |
| 1949 | Careless Hands | Mel Torme |
| 1999 | Where My Girls At? | 702 |
| 1999 | Wish I Could Fly | Roxette |
| 1998 | Hard Knock Life | Jay-Z |
| 1966 | Rainy Day Woman Nos 12 & 35 | Bob Dylan |
| 1963 | Lucky Lips | Cliff Richard |
| 1974 | Smokin' in the Boys' Room | Brownsville Station |
| 2003 | Big Yellow Taxi | Counting Crows |
| 2009 | Whatcha Say | Jason Derulo |
| 1959 | Lipstick On Your Collar | Connie Francis |
| 2002 | A Thousand Miles | Vanessa Carlton |
| 1995 | Conquest of Paradise | Vangelis |
| 1994 | I Like to Move It | Reel 2 Real |
| 1990 | Still Got the Blues (For You) | Gary Moore |
| 1989 | So Alive | Love & Rockets |
| 1975 | Please Mr Postman | The Carpenters |
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