0

Here is the code I am using to try and retrieve an image from a database:

<?php
if($id) 
{
    //please change the server name username and password according to your mysql server setting
    $mysql_server="localhost";
    $mysql_username="myuser";
    $mysql_password="mypass";
    $mysql_database="mydb";
    //connect to database using above settings
    @MYSQL_CONNECT("localhost",$mysql_username,$mysql_password);
    @mysql_select_db("mydb");   
    //select the picture using the id
    $query = "select bin_data,filetype from todo where id=$id";
    //execute the query
    $result = @MYSQL_QUERY($query);
    //get the picture data which will be binary
    $data = @MYSQL_RESULT($result,0,"bin_data");
    //get the picture type. It will change according to file extension it may be either gif or jpg
    $type = @MYSQL_RESULT($result,0,"filetype");
    //send the header of the picture we are going to send
    Header( "Content-type: $type");
    //send the binary data
    echo $data;
};


?>

Instead of displaying the requested image, it displays this icon: (not sure what you call it)... https://i.sstatic.net/16c5e.png

Here are all the columns in my table: https://i.sstatic.net/ravYm.png

I'm pretty positive I'm doing everything right...not sure what's going on. Help anyone? Thanks in advance!

4
  • Get rid of those evil @ signs! And remove the last closing tag: ?> Commented Apr 19, 2012 at 3:24
  • those mysql function names are case sensitive, what/who gave you the idea that you can uppercase them, suppressing errors is also crackers when your trying to debug.. also $id is blank so it will never get that far, you should use isset($id) && is_numeric($id) instead. Commented Apr 19, 2012 at 3:26
  • @LawrenceCherone function names in php are not case sensitive, but I still think they shoould be treated like they are. Commented Apr 19, 2012 at 3:32
  • @tandu is correct, mysql functions are not case sensitive Commented Apr 19, 2012 at 4:09

2 Answers 2

1

IMO only constants should be uppercase (tho I gladly did not know they could be upper),

Anyway Try this:

<?php
$file_not_found = '../not_found_image.jpg';
//Get the id param from GET else null
$id = (isset($_GET['id']) && is_numeric($_GET['id']))?$_GET['id']:null;

if($id != null) {
    $mysql_server="localhost";
    $mysql_username="myuser";
    $mysql_password="mypass";
    $mysql_database="mydb";
    //Connect to database using above settings
    mysql_connect($mysql_server,$mysql_username,$mysql_password) or die(mysql_error());
    mysql_select_db($mysql_database) or die(mysql_error());
    //Select the picture using the id
    $query = "SELECT `bin_data`, `filetype` FROM todo WHERE id=".(int)mysql_real_escape_string($id)." LIMIT 1";
    //Execute the query
    $result = mysql_query($query);

    //Found
    if(mysql_num_rows($result)==1){
        //Get the picture data which will be binary
        $data = mysql_result($result,0,"bin_data");
        //Get the picture type. It will change according to file extension it may be either gif or jpg
        $type = mysql_result($result,0,"filetype");
        //Send the header of the picture we are going to send + cache
        header('Cache-Control: private, max-age='.(60*60*24*365));
        header('Expires: '.gmdate(DATE_RFC1123,time()+60*60*24*365));
        header("Pragma: private");

        header('Content-Type: '.$type);
        header('Content-Length: ' . strlen($data));
        //Send the binary data
        echo $data;

    }else{
    //Not found
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($file_not_found));
    readfile($file_not_found);
    }

}else{
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($file_not_found));
    readfile($file_not_found);
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this..

 header("Content-type: image/gif"); 
 $expires = 60*60*24*14; 
 header("Pragma: public");
 header("Cache-Control: maxage=".$expires);
 header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
 error_reporting(0);
 require_once "class/dbconn.php";
 $id=$_GET['id'];

 $sql="select thumbimage from image where img_id=$id";
 $rs=mysql_query($sql) or die (mysql_error());
 $row =mysql_fetch_array($rs,MYSQL_BOTH);
 $data = $row[0];
 print $data;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.