www.webdeveloper.com
Results 1 to 10 of 10

Thread: cant get image from database

  1. #1
    Join Date
    Jul 2012
    Posts
    41

    cant get image from database

    Sorry for double posting , posted 1 thread in the wrong forum because its obviously more related to php than sql.
    database "dota2"
    table "images"
    id(int11)
    pics(blob)
    ext(varchar4)
    gender(varchar7)

    now i have 1 image stored "id" 5 "pics" [BLOB - 1.4 KiB] "ext" jpg "gender" china
    storing code
    PHP Code:
    $con mysql_connect("localhost","root","aa");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }


    echo 
    "Success! You have connected ";

    $handle fopen("ch.jpg""rb");
    $img fread($handlefilesize('ch.jpg'));
    fclose($handle);
    if (!
    $img)
    {echo
    "not working";}

    $img base64_encode($img);
     
    mysql_connect("localhost","root","aa") or die(mysql_error()); 
     
    mysql_select_db("dota2") or die(mysql_error()); 
    $sql "INSERT INTO images(id, pics, ext, gender) values (null,'$img','jpg','china')";
    mysql_query($sql) or die('Bad Query at 12');
    echo 
    "Success! You have inserted your picture!"
    showing code
    PHP Code:
    header("Content-type: image/jpg");
    $con mysql_connect("localhost","root","aa") or die(mysql_error()); 
    mysql_select_db("dota2");
    $getImage mysql_query("SELECT pics FROM images WHERE id = 5") or die(mysql_error());
    $row_getImage mysql_fetch_array($getImage$con);

    echo 
    $row_getImage['pics'];
    mysql_free_result($getImage); 
    shows error " the image "http://localhost/test2/showimage.php" cannot be displayed ebcause it contains errors", any help ?

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    Have you tried base_64_decode ing what you get back from the DB? I see where you encode it for storage, but I don't see where it gets decoded.

  3. #3
    Join Date
    Jul 2012
    Posts
    41
    I'm not sure if i'm doing it right, i dont yet fully understand the actions of the mysql , i copied this code and tweaked a bit , the part i dont understand is
    PHP Code:
    $getImage mysql_query("SELECT pics FROM images WHERE id = 5") or die(mysql_error());
    $row_getImage mysql_fetch_array($getImage$con); 
    can i use simply $getImage to show the image without $row_getImage = mysql_fetch_array($getImage, $con);?
    nevertheless, decoding failed getting the same error, or im doing it wrong.
    PHP Code:
    header("Content-type: image/jpg");
    $con mysql_connect("localhost","root","aa") or die(mysql_error()); 
    mysql_select_db("dota2");
    $getImage mysql_query("SELECT pics FROM images WHERE id = 5") or die(mysql_error());
    $row_getImage mysql_fetch_array($getImage$con);
    $row_getImage base64_decode($row_getImage);

    echo 
    $row_getImage['pics'];
    mysql_free_result($getImage); 

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    Before you are storing your image bytes you are doing this:
    $img = base64_encode($img);
    You probably want to decode it before you spit out the raw bytes to the browser.

    base64_decode

  5. #5
    Join Date
    Jul 2012
    Posts
    41
    Yes and after your previous post i fixed it to be
    PHP Code:
    $row_getImage mysql_fetch_array($getImage$con);
    $row_getImage base64_decode($row_getImage); 
    as i get it after
    PHP Code:
    $row_getImage mysql_fetch_array($getImage$con); 
    that image is stored in $row_getImage and then i decode it. but the error is the same

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    You just decoded an array in your code...

    $row_getImage = base64_decode($row_getImage);

    echo $row_getImage['pics'];

  7. #7
    Join Date
    Jul 2012
    Posts
    41
    Fair enough. I'm just starting to learn mysql our college neglected to teach us. So now in my understanding i took the value i needed from the array and stored it in $img and decoded it. But the error is the same.

    PHP Code:
    $getImage mysql_query("SELECT pics FROM images WHERE id = 5") or die(mysql_error());
    $row_getImage mysql_fetch_array($getImage$con);
    $img $row_getImage['pics'];
    $imgNew base64_decode($img);
    echo 
    $imgNew;
    mysql_free_result($imgNew); 

  8. #8
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    See: http://php.net/manual/en/function.mysql-fetch-array.php

    Unless my eyes deceive me, this is wrong on a few levels:
    PHP Code:
    $row_getImage mysql_fetch_array($getImage$con);
    $row_getImage base64_decode($row_getImage);
    echo 
    $row_getImage['pics']; 
    Change it to this:

    PHP Code:
    $row mysql_fetch_assoc($getImage);
    $image_data base64_decode($row['pics']);
    print 
    $image_data
    (The change from echo to print is just a personal preference.)
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  9. #9
    Join Date
    Jul 2012
    Posts
    41
    I tried reading on that website but its to raw for me to understand yet.
    I tried changing as you suggested but is till doesn't work. Im tired from trying to do this for almost half a day. Now im just storing the path to the image, in my noob opinion its not as good as storing image itself but at least it works for me.
    Thank you all for trying to help.

  10. #10
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    OK. Well, if you feel like tackling it again tomorrow, give us the public URL of the script you're working on and a public URL for the image itself. Comparing the the difference in output might shed some light on the issue ...

    ... also, it may be worthwhile to re-insert the image in the database. And, if it's small, examine it with the command line client and show us the output of that as well.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles