Is it possible to convert string to varbinary in PHP without using SQL function

I was wondering if it is possible to convert a string to varbinary with PHP to get the same effect as using the SQL function CONVERT(varbinary, 'data')I would like to do this because I use codeigniter and would like to use active records for this query, and because This does not directly use the SQL string, but I need to insert the data into the varbinary field in MS-SQL.

Thank:)

+5
source share
3 answers

you can write the string as binary if you use a fairly modern version of PHP.

$binary = (binary)$string;
PHP 5.2.1 added support

(binary) and b forward prefix support

http://www.php.net/manual/en/language.types.type-juggling.php

+6
public static function str2bin($str) { 
  return '0x'.strtoupper(bin2hex($str));
}
+2

pack

Example to convert {326546, 4356345, 43646346, 366357547} to var-binary as Unsigned int

$ _ BIN = pack ('I *', 326546, 4356345, 43646346, 366357547);

You can find more examples at http://www.php.net/manual/en/function.pack.php

0
source

All Articles