I have a small problem and i'm sure someone have the solution for it I have a binary data converted from a set of unsigned integers
$_BIN=pack('I*', 3563547,6587568,5468456,6458568,4568568);
and i want to insert it in a table that contain a varbinary column
$strSQL = "INSERT INTO table_name (id, ba) VALUES($id, $_BIN)";
for some reason i get the following error
Erreur SQL : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_' at line 1 SQL string : INSERT INTO table_name (id, ba) VALUES(467, ('ùZ¢O ^, ¦EòÇjÞ|²EÎóÇjÞ|²EÎóp>)
Hoping you guys can help me out
This works for me:
DROP TABLE IF EXISTS `binary`;
CREATE TABLE `binary`(
id INT NOT NULL AUTO_INCREMENT,
ba VARBINARY( 100 ),
PRIMARY KEY(`id`)
) ENGINE=MYISAM;
and PHP code (using 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));
UPDATE
See below the code based on your suggested one with named parameters:
<?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;
}
Here is how the table looks like after this is launched:
mysql> select * from `spw_license`;
+------------+------+----------------------------------+
| serial_num | user | ba_struct |
+------------+------+----------------------------------+
| 23 | toto | ♣_ (☻' ∙ вO
^ _жE Є ⌂↕ |
+------------+------+----------------------------------+
1 row in set (0.00 sec)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.