0

I am developing a php script, that reads data from a MYSQL database. Using phpmyadmin i can see that all data is in database and in proper format.

The database consists of text columns and a blob column.

I have now come to read back this data. I have used the following code.

$query = @"SELECT TEXT1, TEXT2, TEXT3, IMAGE FROM EXAMPLETABLE";

Do all the sql calls

Now if i do the following command then it fills the array with the text but null in the image/blob bit

while($row = mysql_fetch_array($result))
{
    $posts[] = array($row['TEXT1'], $row['TEXT2'], $row['TEXT3'], $row['IMAGE']);
}

Result via Safari

[["Gbv","TR","FG",null]]

if i do the following i get the image data.

while($row = mysql_fetch_array($result))
{
    ECHO $row['IMAGE'];
}

Edit So after another look at code (been working far far to hard today) i noticed that i send the array by echo json_encode($posts);

the json_encode is messing up the blob data.

Thanks

1
  • 7
    Every single code example, in your question, has an error in it. Commented Oct 10, 2014 at 20:41

1 Answer 1

1

Create an image from the blob, capture output buffering contents, convert to a jpeg, grab the contents from the buffer, end the output buffering, push the image into the array.

$image = imagecreatefromstring($row['IMAGE']); 
ob_start(); //start capture of the output buffer
imagejpeg($image, null, 80);
$data = ob_get_contents();
ob_end_clean();
$posts[] = array('text'1 => $row['TEXT1'], 'text2' => $row['TEXT2'], 'text3' => $row['TEXT3'], 'image' => $data);

Then do something like:

foreach($posts as $post):
    echo '<img src="data:image/jpg;base64,' .  base64_encode($post['image'])  . '" />';
endforeach;

That should work for you.

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

Comments

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.