<?php
$con = mysql_connect("localhost","root","" );
mysql_select_db("test",$con);
$sql="select image from image";
$res=mysql_query($sql) or die(mysql_error());
$i=1;
while($i < mysql_num_rows($res))
{
$image_id[$i] = mysql_result($res, $i);
echo "<img src=\"".$image_id[$i]."\">";
$i++;
}

?>

I'm using this code to display the image from mysql
but images are shown like this
M¢—‡¤6n幆J1³ŒªÌF·æFLùp1 `‘ã:ÃDVNÒ–¢QSgWñg€?´‘íx3²ûEùÝ ô.l%·ËìÈŒÿAzH­'óÀËË€XƒÍ«L²GçÚ›9ást¸°äŒëŸ]˜¦21ŒÖ|¸E^®ÏJOXX&æàߣéü6bêæ¬Öò7aúQ¡·÷9;Ó-ˆ )ôdx¹cüÖlÒ'j( q1ÌnéW4Ó÷dÇå©uF
Any body please help me whats the error in that code:?:

Recommended Answers

All 2 Replies

Images are the special type to handle with Mysql database.. First of all, check if you uploaded the image to the database correcly by the following code as an example..

<?php
    // the upload function
    function upload(){
 
    if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
 
        // check the file is less than the maximum file size
        if($_FILES['userfile']['size'] < $maxsize)
            {
        // prepare the image for insertion
        $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
        // $imgData = addslashes($_FILES['userfile']);
 
        // get the image info..
          $size = getimagesize($_FILES['userfile']['tmp_name']);
 
        // put the image in the db...
          // database connection
          mysql_connect("localhost", "$username", "$password") OR DIE (mysql_error());
 
          // select the db
          mysql_select_db ("$dbname") OR DIE ("Unable to select db".mysql_error());
 
        // our sql query
        $sql = "INSERT INTO testblob
                ( image_id , image_type ,image, image_size, image_name)
                VALUES
                ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";
 
        // insert the image
        if(!mysql_query($sql)) {
            echo 'Unable to upload file';
            }
        }
    }
    else {
         // if the file is not less than the maximum allowed, print an error
         echo
          '<div>File exceeds the Maximum File limit</div>
          <div>Maximum File limit is '.$maxsize.'</div>
          <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
          <hr />';
         }
    }
?>

You have to upload the images in detail as image size, name, type etc...
And retrieving the image from database also in same manner...

<?php
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
        //connect to the db
        $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
 
        // select our database
        mysql_select_db("testblob") or die(mysql_error());
 
        // get the image from the db
        $sql = "SELECT image FROM testblob WHERE image_id=0";
 
        // the result of the query
        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
 
        // set the header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0);
 
        // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }
?>

This will work fine.. Still any probs, pls let me know...

In the above source., at line 19 specifies an header function.. This is very important...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.