Question

Trying to loop through a test database with images (I know many say not to do this, but it simplifies so much in terms of backups, etc.). I can get the result I want by creating image files on the fly, but there must be a way to do this without creating files. Can someone suggest a syntax I can use without having to create these image files?

Here is what I have working:

<?php
// configuration
$dbhost     = "localhost";
$dbname     = "test";
$dbuser     = "root";
$dbpass     = "";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// query
$sql = "SELECT id,title,author,description,cover FROM books";
$q = $conn->prepare($sql);
$q->execute();

$q->bindColumn(1, $id);
$q->bindColumn(2, $title);
$q->bindColumn(3, $author);
$q->bindcolumn(4, $description);
$q->bindColumn(5, $cover, PDO::PARAM_LOB);

while($q->fetch())
{
file_put_contents($id.".png",$cover);
  echo ("<img src='".$id.".png'><br />, $title, $author, $description,<br/>");
}
?>

I am thinking that we should be able to eliminate the "file_put_contents.." line and in the echo line, replace the

<img src='".$id.".png'>" 

with some php/pdo statement that retrieves the blob and puts it in the proper format. Tried a few things, but have not been successful.

Any suggestions would be helpful!

Was it helpful?

Solution

you do not need to do it in 2 separate files and invoke the script in src attribute of img tag.

Try do this

echo '<img src="data:image/'.$type.';base64,'.base64_encode($image).'"/>';

Where $type is the extension of the image(.png/.jpeg/....) and $image is the binary of the image that you have stored in your DB. in my case I pull the value of the type of image from the db, if you store always the same extension(ex jpeg) you can simply write:

echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';

the method is an alternative to do that with 2 files.

OTHER TIPS

You need to write code in 2 files. As when browser gets <img src="path/to img_file"> code (or code like <script src="....">), etc, a separate request is sent to server by browser at src path value. So, you also need to create file img_file.

Pseudo code is below,

Firstfile.php

<?php ..................... Other code while($q->fetch()) { ?> <img src="image.php?id=<?php echo $id; ?>"><br />, <?php echo "$title, $author, $description<br/>"; } ?>

And at image.php file,

header('Content-Type: image/png');

$id = $_GET['id'];

$query = " ..... where id = '".$_GET['id']."'";

//You need error handling if $_GET['id'] is integer and valid value. I am not writing this error handling code
if($q->row_count!=1){
    //no database row found. It's error.
    echo "Show no image found image. As browser expects image you can not write text here";
    die;
}
//it's valid image, else code is already terminated by above die. 
while($q->fetch())
{
    //show image
     echo $cover;
}
?>

I wrote Pseudo code exact code depend on requirements. If you want .png extension, you can use Apache rewrite. Google it you will get it. When first time i wrote answer, I missed last php ending tag. By the way, last ending tag ?> is optional in php. If you are getting X , it seems first file code is ok. As imag_file is not at server, it's showing 404 error. View source to check whether $_GET['id'] is correct. If id is correct, then you want to write at file img_file.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top