1

I am trying to get an image from my database I use the following code:

  <?php   
    $image_qry=mysqli_query($mysqli,"SELECT * FROM Benutzer");
    $image=mysqli_fetch_assoc($image_qry);
    $get_image=$image['Profilbild']; //This should contain the picture
    header("Content-type: image/jpeg"); 
    echo $get_image;
     ?>

Here is my database structure:

Users

Please help me and tell me how can i output this picture in a commun <div> element

8
  • move header() to top of script and test again Commented Jul 10, 2015 at 9:51
  • 1
    what error do you get. Commented Jul 10, 2015 at 9:52
  • @meh because there was no output before the echo, placing the header() anywhere else has no extra function... Check if the image is actually in the database ... should be a BLOB field that can hold the image... A regular BLOB field is 65k, so if the image were larger than that, you don't have an image Commented Jul 10, 2015 at 9:53
  • @Raphioly-San you're right but it's not the complete script he might echoed header.html in another include somewhere. PS: the size is 53.3 Commented Jul 10, 2015 at 9:59
  • Use echo mysqli_error($mysqli); to check if the query gave errors... Commented Jul 10, 2015 at 10:03

2 Answers 2

1

Instead of storing your image inside of the database create a upload folder then just store the image name into the database and when you want to call it just add the name at the end.

<?php   
    $image_qry=mysqli_query($mysqli,"SELECT * FROM Benutzer");
    $image=mysqli_fetch_assoc($image_qry);

    $link = directory of image;
    $get_image=$link.$image; //This should contain the picture

    echo file_get_contents($get_image);
?>

upload script:

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

reference:

http://www.w3schools.com/php/php_file_upload.asp

Sign up to request clarification or add additional context in comments.

4 Comments

You are correct, that should be done as 'the better way', but that was not the question
and how can i save my picture in that folder?
create a upload script which will upload the pictures, if you want i can include on in the post
@LaurinZiegler already one there, if this helped u dont forget to press the tick :)
0

I think the array of results you got contain the first row in index 0

$get_image=$image[0]['Profilbild']; 

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.