PHP: Retrieve image from MySQL using PDO

后端 未结 4 1812
误落风尘
误落风尘 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: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).

提交回复
热议问题