0

I tried to display image from database,

  • image in directory
  • path in database

    require_once "Connection.php";
    class DisplayDataImageProfile {
    function showImageProfile(){
        $connection = new Connection();
        $conn = $connection->getConnection();
    
        $id = $_GET['id'];
    
        try{
            $sqlDisplay = "SELECT photo FROM frofile WHERE id =$id";
            $getImage = $conn->prepare($sqlDisplay);
            $getImage->execute();
            $getImage->fetchAll(PDO::FETCH_ASSOC);
    
            foreach($getImage as $data){
                header('Content-type: image/jpg');
                // echo "<img src='$data'>";
                echo $data;
            }
    
        }catch (PDOException $e){
            echo "Error : " + $e->getMessage();
        }
    }}
    

after that i call in html page like this :

<img src="DisplayDataImageProfile .php?id=3" align="center" />

i got problem that image cannot retrieve from database using path. other case on webpage the image something like broken image displayed.

for currently i just display one image.

8
  • are you wants to display single image or image in listing? Commented Mar 7, 2016 at 6:23
  • 4
    use $data['photo'] instead of just $data Commented Mar 7, 2016 at 6:23
  • @devpro Post that as the answer ;) Commented Mar 7, 2016 at 6:23
  • 1
    @red Your code is open to SQL injection, you're not preparing correctly. Commented Mar 7, 2016 at 6:24
  • @Matt: thanks for suggestion brother, posted... :) Commented Mar 7, 2016 at 6:26

2 Answers 2

1

You should use the proper method.

Instead of fetchAll() which returns a nested array, you have to use fetchColumn() which returns single value. And you should be using prepared statements properly:

$sql = "SELECT photo FROM frofile WHERE id = ?";
$getImage = $conn->prepare($sqlDisplay);
$getImage->execute([$_GET['id']]);
header('Content-type: image/jpg');
echo $getImage->fetchColumn();

Edit: If you don't have an image itself in database but only path to the image, then you don't need this code at all. Get rid of Display.php and just select your path in the script that is echoing

<img src="Display.php?id=3" align="center" />

and echo selected path instead of Display.php?id=3.

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

5 Comments

now it gave me broken image. on web page like display an image, but broken image.
I added header to my code. And obviously you have to have an image in your database stored properly.
in my database i just store image path, and the image in the folder still gave me broken image.
so you don't need no Display.php script at all!
sorry sir, once again my question, after i followed your code, i got error that id is Undefined. when should i define the variable id ?
0

You need to use $data as:

$data['photo']

instead of just

$data

Because $getImage consist on associative array, so you can not use like your example, you need to use index for print data.

6 Comments

now it gave me broken image. on web page like display an image, but broken image.
@red: check url in view source
something like this : localhost:49185/insertData/AdminPage.php where image will be displayed
@red: and what is the result of print_r($getImage);
still broken image sir.
|

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.