Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Images from MySQL


    Hi All

    I'm trying to store images in a MYSQL db, the images at being uploaded
    for an external program in a blob field.
    I have had a look round and found a few scripts but none of them seem
    to work.
    All the images are JPG, and what I need to do is at a given point in a HTML
    page I want to say "get this image from this table and display it here". The
    trouble
    I have been having is some of the scripts what to print out the
    header("Content-Type: image/jpeg"); but the page will not allow that because
    it
    already done all its header.
    If anybody has some code that does work that would be great ! :)

    Brian


    Brian Guest

  2. Similar Questions and Discussions

    1. Images and mysql
      Hi all, I was wondering If anyone knows how to insert images into MySQL and how to retrieve and display the images... VIA php. Thanks
    2. dynamic images php/mysql
      hello! i'm using a mysql database to store product details. i also have an image field declared as blob. i have the products.php page which i...
    3. Loading images in a movieClip using PHP/MySQL
      Loading images in a movieClip using PHP/MySQL I'm just starting to learn Actioncript, so this may be simple. I would be very happy if anyone could...
    4. Problems with images in MySQL database
      I'm having problem with images becoming corrupted after loading them into a MySQL database and extracting them again. I'm pretty sure it's todo with...
    5. Displaying images from a MySQL Database
      Hi I'm have difficulty displaying images that are stored in a MySQL database. I've tried using the odbc.net provider, the ByteFX provider and the...
  3. #2

    Default Re: Images from MySQL

    Brian wrote:
    > Hi All
    >
    > I'm trying to store images in a MYSQL db, the images at being uploaded
    > for an external program in a blob field.
    > I have had a look round and found a few scripts but none of them seem
    > to work.
    > All the images are JPG, and what I need to do is at a given point in a HTML
    > page I want to say "get this image from this table and display it here". The
    > trouble
    > I have been having is some of the scripts what to print out the
    > header("Content-Type: image/jpeg"); but the page will not allow that because
    > it
    > already done all its header.
    > If anybody has some code that does work that would be great ! :)
    >
    > Brian
    >
    >
    I have done it in the following manner, first in the page where I have all the
    HTML, I have the following link:

    <IMG SRC=<?PHP echo "\"img/showimg.php?pic=" . $picid ."&\""; ?>>

    and the showimg.php looks something like this:

    /* include connect_db() so we make a connection to the database with one
    command */
    include_once("../libs/db.php");
    /* check that we have type and company varibales */
    if(strlen($pic)) {
    /* connect to database */
    $link=connect_db();
    /* Do query */
    $query="SELECT PictureData, PictureType FROM Pictures WHERE PicID='$picid'
    LIMIT 1";
    $result=mysql_query($query);
    /* save picture to variable */
    $picture=mysql_fetch_array($result);
    /* close database */
    mysql_close($link);
    /* send content type and then the data */
    header( "Content-type: " . $picture["PictureType"]);
    echo $picture["PictureData"];
    }
    ?>


    As you see, the picture itself isn't included in the main php page, but has
    it's own file, which is loaded each time a picture from the database is
    displayed and of course, there aren't any limitation how many pictures at the
    time you show (of course long query times and a lagish database can affect this).



    //Aho

    J.O. Aho Guest

  4. #3

    Default Re: Images from MySQL

    I also had this problem, and I solved it similarly to the way J.O. Aho
    did. The problem I am having now is a simple one. If there is no image
    found in the database I want to display a "image not found" graphic,
    like so:

    if (mysql_num_rows($result) < 1)
    {
    header("Content-type: image/gif");
    print("photo_add.gif");
    }

    else
    {
    $row = mysql_fetch_array($result);
    header("Content-type: " . $row['photo_format']);
    print($row['photo_image']);
    }

    The line "print("photo_add.gif");" is where I am having the problem.
    Which is the correct way to phrase this so that photo_add.gif is
    returned to the calling file.

    The code above is called from another html file like so:

    <img src="display_photo.php"></img>

    This displays the image fetched from mysql, but not the photo_add.gif
    image.

    Thanks,

    Esoos
    ebobnar Guest

  5. #4

    Default Re: Images from MySQL

    ebobnar wrote:
    > I also had this problem, and I solved it similarly to the way J.O. Aho
    > did. The problem I am having now is a simple one. If there is no image
    > found in the database I want to display a "image not found" graphic,
    > like so:
    >
    > if (mysql_num_rows($result) < 1)
    > {
    $fullPath="photo_add.gif";
    if ($fd = fopen ($fullPath, "rb")) {
    > header("Content-type: image/gif");
    fpassthru($fd);
    fclose($fd);
    }
    > }
    >
    > else
    > {
    > $row = mysql_fetch_array($result);
    > header("Content-type: " . $row['photo_format']);
    > print($row['photo_image']);
    > }
    >
    > The line "print("photo_add.gif");" is where I am having the problem.
    > Which is the correct way to phrase this so that photo_add.gif is
    > returned to the calling file.
    print is the same as echo and does only handle strings, so to get this to
    work, you would need to do the modifications to your code that I have done, I
    went a step futher and do only send data if the photo_add.gif is found, in the
    case there aren't any picture in the database.
    The $fullPath, is from the location from the php file (../img/photo_add.gif)
    or an absolute path (/home/user12/pictures/website/photo_add.gif).


    //Aho

    J.O. Aho Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139