PHP: Retrieve image from MySQL using PDO

后端 未结 4 1801
误落风尘
误落风尘 2020-12-06 22:38

I am refactoring some old code, including rewriting basic mysql queries to use PDO.

The following works brilliantly in all browsers and for all image types:

相关标签:
4条回答
  • 2020-12-06 22:42

    You can use this code to get image from database using PDO:

    public function getImage($id){
        $sql = "SELECT * FROM images WHERE id = ?";
        $sth = $this->dbh->prepare($sql);
    
        $sth->bindParam(1,$id);
        $sth->execute();
    
    
        $num = $sth->rowCount();
        if( $num ){
            $row = $sth->fetch(PDO::FETCH_ASSOC);
            header("Content-type: ".$row['type']);
            print $row['image'];
            exit;
        }else{
            return null;
        }
    }
    

    type - data type(column name) such as image/png or image/gif
    image - image data(column name) stored in table as LOB
    $this->dbh connection handler
    It works for me but now I need to find out how to use it with JS because result of this function is passed to JavaScript code - so called ajax

    0 讨论(0)
  • 2020-12-06 22:55

    This code displays all the images in the dabase so you can change it and make it do what you want it to do

    getMessage(); } ?>
    <?php 
    #the folder where the images are saved 
    $target = "image_uploads/";
    $image_name = (isset($_POST['image_name']));
    
    $query = ("SELECT user_id ,image_name FROM tish_images");
    $image_show= $con-> prepare($query);
    $image_show->execute();
    while($record =$image_show->fetch(PDO::FETCH_ASSOC)) {
    #this is the Tannery  operator to replace a pic when an id do not have one
    $photo = ($record['image_name']== null)? "me.png":$record['image_name'];
    #display image 
    echo '<img src="'.$target.$photo.'">';
    echo $record['user_id'];
    }
    ?>
    
    0 讨论(0)
  • 2020-12-06 22:58

    You need to paramaterize the imageid value and bind the parameter to PDO::PARAM_LOB:

    $sql = "SELECT image FROM image WHERE imageid=:id";
    $query = $db_conn->prepare($sql);
    $query->execute(array(':id' => $image_id));
    
    $query->bindColumn(1, $image, PDO::PARAM_LOB);
    $query->fetch(PDO::FETCH_BOUND);
    header("Content-Type: image");
    echo $image;
    

    Of course, you'll also want to specify the complete, correct content type (e.g., image/png).

    0 讨论(0)
  • 2020-12-06 23:07

    My personal approach to Image Blobs is using a php file to serve the image, in combination with a GET argument... It's 100% effective and it doesn't involve file creation... For instance, the file to serve the image from the blob would be:

    image.php:

    <?php
    $db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
    if (isset($_GET['imageid'])) {
        $result = $db->prepare("SELECT image FROM image where imageid = ?");
        if ($result->execute(array($_GET['imageid']))) {
            $row=$result->fetch();
            echo $row['pic']; //this prints the image data, transforming the image.php to an image
            }
        } // you can put an "else" here to do something on error...
    ?>
    

    This can be called from you main script... For instance:

    <?php
    $db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
    //... some code to do your job, where you get your imageid from
    $imageid=...
    ?>
    <img src="./image.php?imageid=<?php echo $imageid;?>">
    
    0 讨论(0)
提交回复
热议问题