Forum Moderators: coopster

Message Too Old, No Replies

Displaying images from mysql database

How to display images already stored in mysql database with other row data

         

okonjiaustin

6:56 am on Feb 12, 2008 (gmt 0)

10+ Year Member



I have just been able to successfully insert images into mysql database.
The code is below:

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");

if(isset($_REQUEST['submit']))
{

$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];

if($imgtype=="image/jpeg" ¦¦ $imgtype=="image/jpg" ¦¦ $imgtype=="image/pjpeg" ¦¦ $imgtype=="image/gif" ¦¦ $imgtype=="image/x-png" ¦¦ $imgtype=="image/bmp")
{

$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>
Now I want to retrieve the row data into an html page including the image.

Please let someone help.

Austin

Habtom

7:32 am on Feb 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$query = "SELECT image, image_type from img_tab1 where id=$id";
$result = @MYSQL_QUERY($query);

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

header( "Content-type: $type");
echo $data;

Two points to remember:

  • The data type of the image folder needs to be set to the correct type. You might also need to save the image type in a separate column as shown in the example above.

  • Saving image to a database is not the ideal way of storing images for websites, unless you have a very specific reason for it. Images can be just uploaded to a certain file directory, and the path could be saved in the database, that makes it much more effecient and less maintenance hassles.
  • okonjiaustin

    5:40 am on Feb 13, 2008 (gmt 0)

    10+ Year Member



    Thanks for your reply Habtom,

    While I am trying out the solution you preferred for now, can you kindly give me a code for the separate storage in another directory and save the path in the database as you proposed.

    Thanks, I will appreciate.

    Austin

    okonjiaustin

    5:52 am on Feb 13, 2008 (gmt 0)

    10+ Year Member



    Hello Habtom,

    I have quickly tried the code you gave but what I get is an empty page.

    The code is below:

    <?php
    $errmsg = "";
    if (! @mysql_connect("localhost","root","")) {
    $errmsg = "Cannot connect to database";
    }
    @mysql_select_db("adim");

    $query = "SELECT image, image_type from img_tab1 where id=$id";
    $result = @MYSQL_QUERY($query);

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

    echo $data;

    ?>

    Please you can go through and see where the error is.

    Thanks.

    Austin

    Habtom

    6:10 am on Feb 13, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You will need the header:

    header( "Content-type: $type");
    echo $data;

    okonjiaustin

    2:28 pm on Feb 14, 2008 (gmt 0)

    10+ Year Member



    Hi Habtom,

    I have tried including headers as instructed but I get the error below:

    Warning: Cannot modify header information - headers already sent by (output started at D:\wamp\www\imagesfolder\image_display.php:2) in D:\wamp\www\imagesfolder\image_display.php on line 16

    And this is the code being reffered to in line 16:

    <?php
    $errmsg = "";
    if (! @mysql_connect("localhost","root","")) {
    $errmsg = "Cannot connect to database";
    }
    @mysql_select_db("adim");

    $query = "SELECT image, image_type from img_tab1 where id=$id";
    $result = @MYSQL_QUERY($query);

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

    header( "Content-type: $type");
    echo $data;

    ?>
    Please go through and let me know where I went wrong.

    Thanks

    Austin

    wheelie34

    4:19 pm on Feb 14, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Austin, as Habtom says storing the images in the DB isn't a good idea, if you don't really need to store them in the DB have a look here

    [webmasterworld.com ]

    I have just finished an upload script that adds the data also to a mysql DB, it is very flexible and easy to work with it does what I need and it looks like it will do what you need too.

    The only thing missing on that page is the finished image saving script that now can resize the saved images I finished that yesterday, if you want it just ask and I will post it at the bottom of that thread.

    [edited by: jatar_k at 4:49 pm (utc) on Feb. 14, 2008]
    [edit reason] fixed link [/edit]

    okonjiaustin

    5:03 am on Feb 15, 2008 (gmt 0)

    10+ Year Member



    Hi wheelie34,

    I sure need the script, please do post it.

    Thanks.

    Austin.

    okonjiaustin

    5:10 am on Feb 15, 2008 (gmt 0)

    10+ Year Member



    Hi wheelie34,

    Please when you get the display code let me know also.

    Thanks.

    okonjiaustin

    5:30 am on Feb 15, 2008 (gmt 0)

    10+ Year Member



    Please try and help me with an image download script.

    I have successfully uploaded image to mysql database using a form: This is the code:

    <?php
    $errmsg = "";
    if (! @mysql_connect("localhost","root","")) {
    $errmsg = "Cannot connect to database";
    }
    @mysql_select_db("adim");

    if(isset($_REQUEST['submit']))
    {

    $imgtype=$_FILES['uploadfile']['type'];
    $name=$_REQUEST['name'];
    $address=$_REQUEST['address'];
    $dateofbirth=$_REQUEST['dateofbirth'];

    if($imgtype=="image/jpeg" ¦¦ $imgtype=="image/jpg" ¦¦ $imgtype=="image/pjpeg" ¦¦ $imgtype=="image/gif" ¦¦ $imgtype=="image/x-png" ¦¦ $imgtype=="image/bmp")
    {

    $image=$_FILES['uploadfile']['tmp_name'];
    $fp = fopen($image, 'r');
    $content = fread($fp, filesize($image));
    $content = addslashes($content);
    fclose($fp);
    $sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
    $res=mysql_query($sql) or die (mysql_error());
    }
    }
    ?>

    Now I need to display the row data including the image, can you please offer a code that will solve the problem.

    Thanks

    Austin

    [edited by: jatar_k at 11:22 pm (utc) on Feb. 15, 2008]

    wheelie34

    9:22 am on Feb 15, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    As requested here is the final script that saves the image and data.


    $uploaddir = "../apartments/images/";
    $target = "350";
    foreach ($_FILES as $key=>$value){
    if ($value[size] > '0'){

    $add = "../apartments/images/".$propname."+".$key.".jpg";
    $sql = "insert into images values('', '$propname-$key.jpg', '$propname', '$key')";
    if (mysql_query($sql, $dblink)){
    echo "IMAGE added!<br><br>";
    }else{
    echo "Something was wrong<br><br>";
    }
    copy($_FILES[$key][tmp_name], $add);
    chmod("$add",0644);

    $new_pic = "$propname+$key.jpg";
    $new_image = "$uploaddir$new_pic";

    $sourcefile = "$uploaddir$new_pic";
    $picsize = getimagesize("$sourcefile");
    $source_x = $picsize[0];
    $source_y = $picsize[1];

    if ($source_x > $source_y)
    {
    $percentage = ($target / $source_x);
    } else {
    $percentage = ($target / $source_y);
    }
    $dest_x = round($source_x * $percentage);
    $dest_y = round($source_y * $percentage);

    $targetfile = "$uploaddir".$propname."-".$key.".jpg";
    $jpegqual = 90;
    $source_id = imagecreatefromjpeg("$sourcefile");
    $target_id = imagecreatetruecolor($dest_x, $dest_y);
    $target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
    imagejpeg($target_id,"$targetfile",$jpegqual);
    unlink("$new_image");
    }
    }

    Hope it helps.