1

I am trying to Display an image inserted on my database with a longblob data type. But it does not actually returns the expected output. No images has been shown on my form. And this is what I get on the php script. enter image description here

Also, this is the code I am working with:

PHP:

<?php
    include_once('pConfig.php');
    if (!isset($cID)){
        $cID = filter_input(INPUT_GET, "cIDs");
    }   
    if (!isset($ciCODe)){
        $ciCode = filter_input(INPUT_GET, "ciCodes");
    }
    $stmt = $db->prepare("SELECT * FROM ci_images WHERE ci_ID = ? AND ciCode = ?");
    $stmt->bind_param('is', $cID , $ciCode);
    $stmt->execute();
    $result = $stmt->get_result();
    if (!$result) {
        printf("Error: %s\n", mysqli_error($db));
        exit();
    }
    $json = mysqli_fetch_all($result, MYSQLI_ASSOC);
?>

JAVASCRIPT:

function previewImages(cID){
        var ciCode = window.localStorage.getItem('ciCode');
        var xdata = ({'cIDs': cID, 'ciCodes': ciCode });
        $.ajax({
        type: 'GET',
        url: '../back_php_Code/pPrevImages.php',
        dataType: 'json',
        data: xdata,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
                var cells = eval(response);s
              for (var i=0; i < cells.length ; i ++){
                    $('#iSet').append('<div class="col-lg-4 col-sm-6">'
                    + '<div class="thumbnail">'
                    + '<div class="thumb">'
                    + '<a href="'+ cells[i].Image + '" data-lightbox="9" data-title="' + cells[i].Title + '">'
                    + '<img src="'+ cells[i].Image + '" alt="" class="img-fluid img-thumbnail">'
                    + '</a>'
                    + '</div></div></div>');            
            }
        },
         error: function (error) {
            console.log(error);
        }
   });  
}

I hope someone can help me to do the trick in here, i've been stuck on this on a week. Thank you and Regards

UPDATE:

Result Answer 1

database fields

DBFields

4
  • You aren't echoing anything from php Commented Aug 20, 2019 at 7:05
  • 1
    You have to send the correct header before the data stream. Commented Aug 20, 2019 at 7:11
  • @Cid , i'm sorry sir, that is my fault, did not notice it on my code. Commented Aug 20, 2019 at 7:24
  • But I already Add it, and nothing happens, the same status as before Commented Aug 20, 2019 at 7:25

1 Answer 1

2

Echo your php result. Put this at the end of php file:

echo json_encode($json);

And to display an image blob in that way you try it, you should encode the blob

base64_encode($yourBlob)

send it back to your front-end and inject it to the img src attribute.

<img src="data:image/jpeg;base64,' + dataEncoded + '>

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

7 Comments

Hi Sir, Where should I put base64_encode If I already echo the final result? Can you add this answer sir on my code? Thank you.
Hi Sir, After clearing the cache of my web browser. This is what i get now. What should I do then? See UPDATED Image
Before echoing modify $json $json['theBlobFieldName'] = base64_encode($json['theBlobFieldName']);
Ok Sir, I'll give it a shot. But Sir, Can you see the result after adding your answer?
How do you store the images to the db?
|

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.