how to retrieve images from a mysql database?

Discussion in 'PHP and MySQL' started by awariss, May 19, 2005.

  1. awariss

    awariss Guppy

    hi,

    i can store images in a mysql database, but i am having trouble to retreive the image to show it in a webpage.

    this is what i did :
    ___________________________________________________

    @MYSQL_CONNECT("localhost","root","password");

    @mysql_select_db("binary_data");

    $query = "select image,i_filetype from product where product_code = '". $pid ."'";
    $result = @MYSQL_QUERY($query);

    $data = @MYSQL_RESULT($result,0,"image");
    $type = @MYSQL_RESULT($result,0,"i_filetype");

    echo "<BR><BR>";

    echo "<img scr='$data'>";

    _________________________________________________

    what i get from this is all text and characters.(like when we open image file in a notepad)

    can anyone please please help me out! ?(
  2. Stephen

    Stephen US Operations Staff Member

    I am not sure the code you need to use, but you need to use BLOB which is for Binary data.
  3. SubSpace

    SubSpace Bass

    Assuming you're using the proper data types so the data is undamaged: the way you're outputting the image makes no sense at all ;)

    echo "<img scr='$data'>";

    You're using the data as a URL in a HTML tag, and you typo'd the src attribute.

    Assuming $type is a valid MIME type, you'd have to get rid of the current echo statements and do something like:

    PHP:
    header('Content-type: ' $type);
    echo 
    $data;
  4. awariss

    awariss Guppy

    thanks for the reply !

    yes! i used BLOB for the image field. and i am trying to show the image in a web page within an html file.

    when i tried using:
    ____________________________________________________

    <?
    $query = "select image,i_filetype from product where product_code = '". $pid ."'";
    $result = @MYSQL_QUERY($query);

    $data = @MYSQL_RESULT($result,0,"image");
    $type = @MYSQL_RESULT($result,0,"i_filetype");


    header('Content-type: ' . $type);
    echo $data;

    ?>
    ____________________________________________________

    it gives me :

    Warning: Cannot modify header information - headers already sent by (output started at C:\apachefriends\xampp\htdocs\view_product.php:119) in C:\apachefriends\xampp\htdocs\view_product.php on line 127
    ????JFIF``??C    $.' ",#(7),01444'9=82<.342??C  2!!22222222222222222222222222222222222222222222222222????"?? ...............and so on!!!


    i tried using the
    " header('Content-type: ' . $type); " at the top before html, and still it gives out the same but without the warning.

    this is how the image is saved in the database:
    ___________________________________________________
    image i_filename i_filesize i_filetype
    ___________________________________________________
    [BLOB - 6.1 KB] 1.JPG 6291 image/pjpeg
    ___________________________________________________

    any help would really be appreciated.
  5. SubSpace

    SubSpace Bass

    There shouldn't be any HTML in this particular PHP script.

    You should split this script in two, view_product.php would contain your page with HTML. Somewhere in that page you'd use

    Code:
    <img src="product_image.php?pid=<?php echo $pid ?>">
    Then in product_image.php you retrieve the binary data for the picture, set the Content-type header and output the image data.
  6. awariss

    awariss Guppy

    THANKS A LOT...... SubSpace :]

    it worked....!

    i have another problem. ?(

    i am trying to get the maxmimum value in a particular field and increment the value by one and place it again in that field.

    the maximum value in (p_id) is 100.... what i want to do is take that 100 and add 1 to it to make it 101..

    i am using this..to check if it takes tha maximum value and adds one to it.
    ____________________________________________________

    $sql="select max(p_id) from product";

    $result =mysql_query($sql) or die (mysql_error());
    if ($result)
    {
    echo "$result";
    $result++;
    echo "<BR><BR>";
    echo "$result";
    exit;
    }

    ___________________________________________________

    it does not work...gives out something like.

    Resource id #3

    Resource id #3



    please help me out here.!
  7. chazm

    chazm Guppy

    thanks alot Hammerhead;
    you saved my life only with one statement
    'There shouldn't be any HTML in this particular PHP script'

    i kept every thing correct but still problem was there; so after removing those htmls from my php file the thing got worked; it took me three days down the road without answers.
    thanks alot
  8. skypanther

    skypanther Exalted Code Master!

    Why not just use an autonumber field type and let MySQL do this for you? I'm guessing you're trying to use this field as a unique ID or counter, right. Right? If so, in general it's best to rely on the DB or use some sort of stored procedure. Doing it in PHP as you're trying will fail in a multi-user update situation.

    User A starts your script and retrieves the old ID - let's say 100
    User B does the same and also gets 100
    User A's script finishes and stores 101 in the field
    User B's script also finishes and stores 101 in the field.

    Unless you apply some sort of locking & blocking, your data is going to get messed up like this.

    As for your code, it should be something like:

    $sql="select max(p_id) AS theMax from product";
    $result =mysql_query($sql) or die (mysql_error());

    if ($result)
    {
    $row = mysql_fetch_assoc($result)
    $theCount = $row['theMax'];

    echo $theCount;
    $theCount++;
    echo "<BR><BR>";
    echo $theCount;
    exit;
    }

    Tim
  9. satish21114

    satish21114 Guppy

    hi Hammerhead Shark
    thanks a lot lotttttttttttt
    i have also problem that can't display picture from mysql to html page by php i was got error that modify header error.. i try to solve this problem but no-one can solve this problem.. but by using your idea i have solved problem so thank a lot

Share This Page

JodoHost - 26,000 hosting end-users in 100 countries
Plesk Web Hosting
VPS Hosting
H-Sphere Web Hosting
Other Services