$con = mysql_connect("localhost","root","" );
$sql="select image from image";
$res=mysql_query($sql) or die(mysql_error());
while($i < mysql_num_rows($res))
$image_id[$i] = mysql_result($res, $i);
echo "<img src=\"".$image_id[$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:?:

7 Years
Discussion Span
Last Post by Dragonbaki

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..

    // 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)
                ('', '{$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
          '<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...

    // just so we know it is broken
    // 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
    else {
        echo 'Please use a real id number';

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.