Jump to content

Storing images in Mysql


willaguila

Recommended Posts

Can anyone help me make sense of this? I'm trying to store images in a mysql database. I got the storing script working properly but the retrieval script below i don't know how to use to place the image on a web page?

 

 

<?php

//Shows the Picture showfile.php

/*** some basic sanity checks ***/

if(filter_has_var(INPUT_GET, "image_id") !== false && filter_input(INPUT_GET, 'image_id', FILTER_VALIDATE_INT) !== false)

    {

    /*** assign the image id ***/

    $image_id = filter_input(INPUT_GET, "image_id", FILTER_SANITIZE_NUMBER_INT);

    try    {

        /*** connect to the database ***/

        $dbh = new PDO("mysql:host=domain", 'dbase', 'password');

 

        /*** set the PDO error mode to exception ***/

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 

        /*** The sql statement ***/

        $sql = "SELECT image, image_type FROM testblob WHERE image_id=$image_id";

 

        /*** prepare the sql ***/

        $stmt = $dbh->prepare($sql);

 

        /*** exceute the query ***/

        $stmt->execute();

 

        /*** set the fetch mode to associative array ***/

        $stmt->setFetchMode(PDO::FETCH_ASSOC);

 

        /*** set the header for the image ***/

        $array = $stmt->fetch();

 

        /*** check we have a single image and type ***/

        if(sizeof($array) == 2)

            {

            /*** set the headers and display the image ***/

            header("Content-type: ".$array['image_type']);

 

            /*** output the image ***/

            echo $array['image'];

            }

        else

            {

            throw new Exception("Out of bounds Error");

            }

        }

    catch(PDOException $e)

        {

        echo $e->getMessage();

        }

    catch(Exception $e)

        {

        echo $e->getMessage();

        }

        }

  else

        {

        echo 'Please use a real id number';

        }

?>

 

 

 

 

 

<?php

 

// View the pictures view.php

/*** Check the $_GET variable ***/

if(filter_has_var(INPUT_GET, "image_id") !== false && filter_input(INPUT_GET, 'image_id', FILTER_VALIDATE_INT) !== false)

    {

    /*** set the image_id variable ***/

    $image_id = filter_input(INPUT_GET, "image_id", FILTER_SANITIZE_NUMBER_INT);

  try    {

          /*** connect to the database ***/

          $dbh = new PDO("mysql:host=domain dbname=dbase", 'user', 'password');

 

          /*** set the PDO error mode to exception ***/

          $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 

          /*** The sql statement ***/

          $sql = "SELECT image_type, image_size, image_name FROM testblob WHERE image_id=".$image_id;

 

          /*** prepare the sql ***/

          $stmt = $dbh->prepare($sql);

 

          /*** exceute the query ***/

          $stmt->execute();

 

          /*** set the fetch mode to associative array ***/

          $stmt->setFetchMode(PDO::FETCH_ASSOC);

 

          /*** set the header for the image ***/

          $array = $stmt->fetch();

 

          /*** the size of the array should be 3 (1 for each field) ***/

          if(sizeof($array) === 3)

              {

              echo '<p>This is '.$array['image_name'].' from the database</p>';

              echo '<img '.$array['image_size'].' src="showfile.php?image_id='.$image_id.'">';

              }

          else

              {

              throw new Exception("Out of bounds error");

              }

          }

      catch(PDOException $e)

          {

          echo $e->getMessage();

          }

      catch(Exception $e)

          {

          echo $e->getMessage();

          }

      }

else

      {

      echo 'Please use a valid image id number';

      }

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.