Varbinary field not working in PHP 7

I am using PHP to make a call to MSSQL server.
It is returning a field type ‘varbinary’ that I am using to hold the data on an image

Then I would use the hex field to convert it and display it

This is how I call the database

 $conn = new PDO("sqlsrv:Server=$myServer;Database=$myDB", $myUser, $myPass);
	$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
	}
$stmt = $conn->prepare("exec KVEVI_GetImage_IndId  @IndId=?" ); 
	$stmt->bindParam(1, $id); 
	$stmt->execute();  
	$photo =  $stmt->fetch(PDO::FETCH_ASSOC); 
$photo= hex2bin(	$photo);
 echo '<div class="img-outer"><img class="img img-responsive img-circle" src="data:image/jpeg;base64,'.base64_encode( $photo ).'"/></div>'; 

The code worked perfectly with PHP 5.6
Now the server has been upgraded to PHP 7.2 and now the binary field is coming back as gibberish and the hexbin is throwing an error that it’s not hexdecimals.

What do I need to do to make this work now?
Thank you

Try the following,

$stmt = $conn->prepare("exec KVEVI_GetImage_IndId  @IndId=?" ); 
$stmt->bindParam(1, $id); 
$stmt->execute(); 
$stmt->setAttribute(constant('PDO::SQLSRV_ATTR_ENCODING'), PDO::SQLSRV_ENCODING_BINARY);
$photo =  $stmt->fetch(PDO::FETCH_ASSOC);

PHP7 broke the binary encoding default

It’s still not working. It’s telling me hex2bin won’t work on that field.

Was it inserted as hex or binary? That should give you the binary that is stored

If I try the simple select from SQL manager, it returns the correct information as binary

When I try to connect using the PHP, it’s coming back gibberish.
So much more so, if I switch my PHP INI file to php5.6 it works, when I switch to PHP 7, it doesn’t work anymore.

The data is correct. Somehow the PHP is messing it up

Is the driver up to date?

We are up to date;
Does this help you? We are using non thread safe and x86 which is 32bit?
1

2

3

Struggling to figure out any other thing I can see that may correct the issue.

Then I had this brilliant idea to convert the column in my sql procedure and send it our as varchar(max)
That worked and the output I am getting is now correct and the characters are showing through.

But now when I try to convert it back to an image

 echo '<div class="img-outer"><img class="img img-responsive img-circle" src="data:image/jpeg;base64,'.base64_encode(hex2bin(	$photo['IND_Thumb'] ).'"/></div>';

Now I am getting a message not a valid binary.
Is there an easier way to get around the new issue?

I’m wondering if you still need the hex2bin call. Is the bin2hex called for the insert?

The original field in my database is an image converted to binary.
If i convert it to nvarchar(max) so I can view the characters through PHP, how can I get it to display back as an image?

You should be able to convert it back.

What’s the best way to do that?

This is not giving me a valid photo

$string= 	$photo['IND_Image'];
$bin = implode(unpack("H*", $string));
$newphoto= hex2bin(	$bin);
 echo '<div class="img-outer"><img class="img img-responsive img-circle" src="data:image/jpeg;base64,'.base64_encode( $newphoto ).'"/></div>';
Sponsor our Newsletter | Privacy Policy | Terms of Service