how to pack() several formats and write the binary string into a db?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I tried to pack eight integer values and one string into one binary
string, which I would like to store in a mysql db.
I encountered two problems doing this:

$this->packed = pack('N8A*', $this->value1, $this->value2,
$this->value3, $this->value4, $this->value5, $this->value6,
$this->value7, $this->value8, $this->stringvalue);
$array = unpack('N8A*', $this->packed);

Array ( [A*1] => 0 [A*2] => 0 [A*3] => 2 [A*4] => 0 [A*5] => 0 [A*6]
=> 91 [A*7] => 0 [A*8] => 1 )
instead of packing the string also. 'N8 A*' doesn't work at all.

I started storing only the seven integers in the binary string and
wanted to write them into the database.
I used a column "packed varchar(50) binary not null default '';".
To store the data I used
$packed = addslashes($classname->packed);
mysql_query("UPDATE $table SET packed='$packed' WHERE ...");
as well as without addslashes().
I also changed the column type to "packed tinyblob not null;" what I
didn't liked much as I would like to reduce the amount of space as
much as possible.
Whatever I did, I got a string back that looked simular when I print
it, but it didn't unpack() to the former values.

Now I have two questions:
 - How can I pack several data types into one binary string (8
integers and one string) using pack()? How should the format string
look like?
 - How can I store such a binary string in a database?

Thanks in advance,

Re: how to pack() several formats and write the binary string into a db?

Quoted text here. Click to load it

Read the manual carefully. You have to assign names to the variables you're

Site Timeline