这对我有用:
DROP TABLE IF EXISTS `binary`;
CREATE TABLE `binary`(
id INT NOT NULL AUTO_INCREMENT,
ba VARBINARY( 100 ),
PRIMARY KEY(`id`)
) ENGINE=MYISAM;
和 PHP 代码(使用 PDO):
<?php
$db = new PDO('mysql:dbname=test;host=localhost:4040','xxx','xxxx');
$_BIN=pack('I*', 3563547,6587568,5468456,6458568,4568568);
$rs = $db->prepare("INSERT INTO `binary`(`ba`) VALUES(?)");
$rs->execute(array($_BIN));
更新
根据您建议的带有命名参数的代码,请参见下面的代码:
<?php
$_SNB = 23;
$_USR = "toto";
$_BIN = pack('I*', 24325, 2556456, 547577, 675746, 535646, 4564575, 575474, 4735);
$db = new PDO('mysql:host=localhost:4040;dbname=test','xxxx','xxxx');
$db->exec('SET CHARACTER SET utf8');
$rs = $db->prepare("INSERT INTO `spw_license` (`serial_num`, `user`, `ba_struct`)
VALUES(:serial_num, :username, :ba_struct)");
$rs->bindValue(':serial_num', $_SNB, PDO::PARAM_INT);
$rs->bindValue(':username', $_USR);
$rs->bindValue(':ba_struct', $_BIN, PDO::PARAM_LOB);
try {
if(!$rs->execute()) {
var_dump($db->errorInfo());
}
} catch(Exception $e) {
echo 'got Exception: ' . $e->getMessage() . PHP_EOL;
}
这是启动后表格的样子:
mysql> select * from `spw_license`;
+------------+------+----------------------------------+
| serial_num | user | ba_struct |
+------------+------+----------------------------------+
| 23 | toto | ♣_ (☻' ∙ вO
^ _жE Є ⌂↕ |
+------------+------+----------------------------------+
1 row in set (0.00 sec)