Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:28:15+00:00 2026-05-22T11:28:15+00:00

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

  • 0

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:

$query = 'SELECT image FROM image WHERE imageid=' . $image_id;
$result = mysql_query($query, $db_conn); querycheck($result);
header("Content-type: image");
echo mysql_result($result, 0);

Unfortunately, however I rewrite it using PDO, it doesn’t work. I’ve been through the entire PDO documentation and the standard web search, but none of the advice/solutions work.

How can one easily fetch and image from MySQL using PDO and display it?

Edit 1:

Matthew Ratzloff gives what should be the obvious answer below, but it does not work.
Here is the actual code that I test using PDO (and I have tried many variants/parameters):

$connectstring_temp = 'mysql:host=' . $A . ';dbname=' .$B;
$dbh_temp = new PDO($connectstring_temp, $login, $password);
#$dbh_temp->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
#$dbh_temp->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);

$sql = "SELECT image FROM image WHERE imageid=" . $image_id;
$query = $dbh_temp->prepare($sql);
$query->execute();

$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image");
echo $image;

I’ve kept the same syntax, although for the final code $image_id needs to be passed as a parameter. The code above does NOT work. PDO works fine for all other queries of all types.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:28:16+00:00Added an answer on May 22, 2026 at 11:28 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst refactoring some old code I realised that a particular header file was full
While refactoring some old code I have stripped out a number of public methods
I'm refactoring some old JavaScript code and there's a lot of DOM manipulation going
I'm currently refactoring/tidying up some old C code used in a C++ project, and
I am currently refactoring some old code. I am looking for directions on the
I unfortunately was doing a little code archeology today (while refactoring out some old
Okay, strange question time! I'm refactoring some old C++ code that declares a bunch
I'm refactoring some code in C++, and I want to deprecate some old methods.
I was refactoring some old code (by other people) and I came across the
I am currently refactoring some of my old code that is pretty terrible. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.