Link to home
Start Free TrialLog in
Avatar of rivkamak
rivkamakFlag for United States of America

asked on

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
$photo= hex2bin(	$photo);
 echo '<div class="img-outer"><img class="img img-responsive img-circle" src="data:image/jpeg;base64,'.base64_encode( $photo ).'"/></div>'; 

Open in new window


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
Avatar of David Favor
David Favor
Flag of United States of America image

Tip: Try writing out the $photo data into a file, before calling hex2bin(), then test the output file to ensure all's well.

If not, then pull the data out of the database directly into a file using a SELECT statement.

You can test your image using the command...

file /path-to-image.jpg

Open in new window


Which should report your file is a JPEG image. This will tell you where to start debugging.
Avatar of rivkamak

ASKER

When I pull the data from the DB on  PHP5.6 if I echo the actual $photo['field'] 

 I get neat binary text X0000456


When I upgrade to PHP 7, it's the exact same DB call, but I get back when I echo


�O�L�J"�zdi��������x���ky��BGc�� � � � � � � � � � � � � � � � � � � � � � �

This is how I call the EXEC statement

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

Open in new window


I need the field that is returned $photo['field'] to be binary

I saw somewhere I need to include this parameter   PDO::SQLSRV_ENCODING_BINARY


I've seen online how to utilize it as an INPUT field on an Insert statement, But I can't figure out how to put it on an OUTPUT field. 


Any ideas? 

Any other advice possibly on how to connect when pulling non tex values?

ASKER CERTIFIED SOLUTION
Avatar of rivkamak
rivkamak
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial