www.webdeveloper.com
Results 1 to 2 of 2

Thread: Cant get image from the database

  1. #1
    Join Date
    Jul 2012
    Posts
    41

    Cant get image from the database

    I'm trying to simply store and then show image in database.
    database name "dota2"
    table "images"
    id(int11)
    pics(blob)
    ext(varchar4)
    gender(varchar7)

    storing code, it works, well atleast it adds data to the database i dont know if the data is proper.
    PHP Code:
    <?php

    $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!";

    ?>
    but now i cant get it from my database(cant show it), getting error "The image "http://localhost/showing/" cannot be displayed because it contains errors"
    get image code
    PHP Code:
    <?php
    $link 
    mysql_connect("localhost","root","aa");
    mysql_select_db("dota2");
    $sql "SELECT pics FROM images WHERE id=1";
    $result mysql_query("$sql");
    header("Content-type: image/jpeg");
    echo 
    mysql_result($result0);
    mysql_close($link);

    ?>
    any help ?
    edit: the id is definitely 1
    Last edited by Stobor; 07-26-2012 at 11:12 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,174
    If you're going to base64 encode it when you insert it, then you'll have to decode it when you retrieve it -- though frankly I wouldn't bother. (Just use a BLOB type for the column).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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