0

I have a problem to download images stored in my Mysql database as a blob. I tried already a lot of solutions which I found in other posts but the result is every time the same:

"[{"image":null},{"image":null},{"image":null}]"

I reduced my code more and more to get the spot as small as possible but nothing. This is my last try which is actually a copy of this post :Empty PHP output from MySQL database for a longblob

<?php
 
// Create connection
$con=mysqli_connect("Server","User","Pw","DBName");
 
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
    
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT CAST(engineerSignature as CHAR(1000000) CHARACTER SET utf8) as engineerSignature FROM tblservreport";
    
if ($result = mysqli_query($con, $sql)){
    
    $resultArray = array();
    $tempArray = array();
 
    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {       
            $tempArray = $row;
            array_push($resultArray, $tempArray);

    }
    
    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}

mysqli_close($con);
?>

but it also didn’t work. Do I look on a wrong place? I’m sure that the images are OK because I have another program on a Windows PC and on that I download it directly without the way through PHP and it works fine. I read a lot of answers about why it’s not a good way to store an image in a DB like this but this is not my choice I have to follow this way.

I hope someone else can see what I am missing or has an idea what I can try.

BDL
  • 21,052
  • 22
  • 49
  • 55
Park
  • 5
  • 3

2 Answers2

0

you must Encode the binary data as "base64" before you generate the "JSON".

$obj = base64_encode($resultArray);
$obj_Json = json_encode($obj);
var_dump($obj_Json);

You can also try this steps to find out the errors.

  1. instead of use echo use var_dump($resultArray) to see whats going on in that array.

  2. upload your image then go to your mysql admin panel and see that whether file is uploaded successfully and showing as blob in the database. If not you have problem in upload files

  3. make sure your sql query is correct.

and also your Question is not clear, so I have given you some basic tip.

  • Thank kayes ibna Qayum, i try to encode it to base64 and change echo to var_dump. unfortunately do i not know that so i down't understand the result eader. The result ist "string(4) "null"" what means that? – Park Aug 02 '20 at 15:33
  • To point 2. the upload workes fine, i can see it as Blob in my Database and i also can open it from the Other device. to point 3. i changed it to "Select * FROM ..." and its the same – Park Aug 02 '20 at 15:43
0

OK now I know var_dump, Thank you for the hint. I tried as already written var_dump($obj_Json) and got "string(4)"NULL"" but if I try var_dump($row) then i got "object(stdClass)#3 (1) { ["Image"]=> string(22004) "�PNG IHDR\�ho��sRGB���gAMA�� �a pHYs���o...... "

Park
  • 5
  • 3