PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.50 USD)

More information
Related Books
PHP and MySQL Web Development (4th Edition)

PHP and MySQL Web Development (4th Edition)

PHP and MySQL are popular open-source technologies that are ideal for quickly developing...

Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)

Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)

If you know HTML, this guide will have you building interactive websites quickly. You'll learn...
Browse Articles
Ajax (4), Amazon (1), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (8), onbeforeunload (1), OOP (1), PHP (35), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (2), SimpleXML (1), Smarty (5), SOAP (2), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (7), Zend_Loader (1), Zend_Registry (1), Zend_Search_Lucene (1)

Storing Images In MySQL

Note: This article has been replaced by Storing Images in MySQL Revisited (by Quentin Zervaas, 2010). We've left this article here for historical purposes, but we strongly recommend you use the newer article instead.

Displaying The Image

Now we have the ability to get the images into the database, we need to be able to get it out.

Here we see a simple script to display an image based on its image_id in the database. Of course you should do more checking on the value of $_GET['image_id'] here. We will call this file view.php.

Listing 6 file.php
<?php
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        //connect to the db
        $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
 
        // select our database
        mysql_select_db("testblob") or die(mysql_error());
 
        // get the image from the db
        $sql = "SELECT image FROM testblob WHERE image_id=0";
 
        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
 
        // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);
 
        // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

In This Article


Tagged in ,

Article History

May 30, 2005
Initial article version
Jan 18, 2008
Updated listing 5 to not include non-existent conf.php