0

I am trying to display an image using javascript from mysql table and also using php. I have a table named ci_images and has a fields id, ci_ID, ciCode, Title, image. After getting the rows using the code sql statement below, I want to display it using javascript. But i am not getting the right result on this code. It always returns an object in Console of the browser (image). Please see code below

FIELDS:

id - int

ci_ID - int

ciCode - VarChar

Title - VarChar

image - LongBlob

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);
    echo json_encode($json);
?>

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);
            alert(JSON.stringify(cells));
            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="..\files\assets\images\gallery-grid\1.png" data-lightbox="9" data-title="' + + '">'
                    + '<img src="'+ + '" alt="" class="img-fluid img-thumbnail">'
                    + '</a>'
                    + '</div></div></div>');            
            }
        },
         error: function (error) {
            console.log(error);
        }
   });  
}

Return on Console.

enter image description here

Can someone help me to do the trick for this, I am stuck on this.

Thanks and Regards

Also, I have found something similar, but it does not work because , it only uses php and html. I think. Again Thanks

Link:

PHP display image BLOB from MySQL

1 Answer 1

2

From PHP you are returning json string, so in javascript you do not have to eval the result, but decode it with JSON.parse.
Other than that you are returing a single record, not a list, why you are doing a for loop in javascript?

Your code will become:

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) {
            // here you transform your response in a js Object
            var row = JSON.parse(response);

            $('#iSet').append('<div class="col-lg-4 col-sm-6">'
                    + '<div class="thumbnail">'
                    + '<div class="thumb">'
                    + '<a href="..\files\assets\images\gallery-grid\1.png" data-lightbox="9" data-title="' + row.YOUR_FIELD_FROM_DATABASE + '">'
                    + '<img src="' + row.YOUR_FIELD_FROM_DATABASE + '" alt="" class="img-fluid img-thumbnail">'
                    + '</a>'
                    + '</div></div></div>');
        },
         error: function (error) {
            console.log(error);
        }
   });
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi sir, I just noticed that you have remove the looping part?
I wrote on the post. You are returning a single record of the database, why you should do a loop?
Ahh! ok Sir. but I am trying to get more than 1 record, the ci_ID and ciCode is foreign to. link from other tables.
@AlexAbulencia, if the solution helps you, please accept the answer. May be you need to add the looping part as well.
@AlexAbulencia are you sure you are getting several records? your select gets just 1.
|

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.