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

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

More information
Related Books
PHP and MySQL Web Development (4th Edition) (Developer's Library)

PHP and MySQL Web Development (4th Edition) (Developer's Library)

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

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

It hasn't taken Web developers long to discover that when it comes to creating dynamic,...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Storing Images In MySQL

Displaying All The Information

Now we have the basic building blocks, we could create a script to see all the information about the image that we have stored in our table.

The trick here is to avoid sending headers twice. To do this we use two files. The first we will call file.php and we use the above view.php in our HTML img tag. Of particular importance here is not to make the mistake mentioned at the beginning of this tutorial of doing something silly like SELECT * FROM testblob.

This is not required and would slow us down in our query because we would need to store the image itself in memory when we are not going to use it yet. So we simply SELECT the other information from the fields that we do want.

Listing 7 view.php
<?php
    // again we check the $_GET variable
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        $sql = "SELECT image_type, image_size, image_name FROM testblob WHERE image_id=".$_GET['image_id'];
 
        $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
 
        // select our database
        mysql_select_db("testblob") or die(mysql_error());
 
        $result = mysql_query($sql)  or die("Invalid query: " . mysql_error());
 
        while($row=mysql_fetch_array($result)) {
            echo 'This is '.$row['image_name'].' from the database<br />';
            echo '<img '.$row['image_size'].' src="view.php?image_id='.$_GET['image_id'].'">';
        }
    }
    else {
        echo 'File not selected';
    }
?>

From the above script we can see the first image from the database. At the top of the page it has echoed a little message and the image name from the database.

You could, as mentioned earlier store other information about the image in the same way and display it. In this script we also use the image_size field from that we had gained from getimagesize(). This stores the image width and height in a string that we can use in img tags.

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