Home jQuery

How To Use jQuery Lightbox With A Database? Step by Step Guide!

Today I’m gonna show you how to use Lightbox while getting photo information from your database. I think this is great if you wanna have something like a dynamic photo gallery in your site.

How To Use Lightbox With A Database

Step 1: Prepare your Database. We’ll have something like this:

  `title` varchar(32) NOT NULL,
  `description` text NOT NULL,
  `filename` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)

-- Dumping data for table `photos`

INSERT INTO `photos` (`id`, `title`, `description`, `filename`) VALUES
(1, 'Mt. Batulao', 'Mt. Batulao''s New Trail', 'Mt-Batulao.jpg'),
(2, 'Mt. Polis', 'A few klicks outside Bontoc going up to Mt Polis', 'Mt-Polis.jpg'),
(4, 'Chocolate Hills 1', 'The wonderful chocolate hills', 'chocolatehills-1.jpg');

Step 2: Download Lightbox here.

Step 3: Unzip it on your web directory.

Step 4: Prepare your database configuration file. I have config_open_db.php file, just for database connection.

Step 5: Create a images folder, we’ll assume that your photos are stored here. In this tutorial, I used some photos from this link.

Step 6: We’ll have the following codes on our index.php file.

<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How To Use jQuery Lightbox With A Database</title>

    <link rel="stylesheet" type="text/css" href="js/css/jquery.lightbox-0.5.css" media="screen" />

<div>You may click the images below.</div>
<div id="gallery"> <!-- id to detect images for lightbox -->

include 'config_open_db.php'; // Database Connection
$sql = "select * from photos"; // Query the photos
$rs = mysql_query( $sql );

while ( $row = mysql_fetch_array( $rs ) ){ // Loop through the records
	$file_name = $row['filename'];
	$title = $row['title'];
	$description = $row['description'];

	// We will append the $file_name variable to href and src attributes
	// to identify what image is being selected/shown
	// The rel='lightbox[philippines]' <a> attribute is
	// needed to use lightbox for set of images
	// It should be stored under one name, we gave the name 'philippines'
	// we also included the title and decription on the title attribute
	// so it will be shown on the overlay
	echo "<a href='images/$file_name' rel='lightbox[philippines]' title='$title - $description'>";
		echo "<img src='images/$file_name' width='150' height='100' />";
	echo "</a> ";

<div class='back-link'>
	<a href='https://www.codeofaninja.com/2010/10/how-to-use-lightbox-with-database.html'>Back to tutorial page</a>

<script type="text/javascript" src="js/js/jquery.js"></script>
<script type="text/javascript" src="js/js/jquery.lightbox-0.5.js"></script>

<script type="text/javascript">
// script to activate lightbox
$(function() {
	$('#gallery a').lightBox();


There are also lots of things that you can configure in jQuery Lightbox such as overlay background color, opacity, navigation behavior, button images, border size, resize speed, keyboard navigation, and etc.

You can find that in jquery.lightbox-0.5.js file in the js/ directory.

Download Source Code

You can download all the code used in this tutorial for only $9.99 $5.55!


has been added to your cart!

Powered by Easy Digital Downloads

Thank you!

have been added to your cart!

Powered by Easy Digital Downloads

Thank you for learning from our post about: How To Use jQuery Lightbox With A Database? Step by Step Guide!