【发布时间】:2011-11-09 05:56:46
【问题描述】:
尝试向 PostgreSQL 写入一个 12 字节的字符串
我们的代码计算了一个 $number。然后我们不想将其转换为十六进制,用零填充它并将其写入 PostgreSQL bytea 字段。简单吧?
(例如)希望它返回:\x000000002257('\x' + 12 字节) 即,数字 8791 的左侧填充十六进制表示:
$number = 8791;
$hexnumber = str_pad(dechex($number), 12, '0', STR_PAD_LEFT);
$packed_hex = pack('h*', $hexnumber);
// BOTH of below produce: 000000002257
pg_raise('notice', "hexnumber: ".$hexnumber);
无法获得这些查询中的 任何 项来更新我希望的 bytea。救命!
//$query = ("UPDATE blobtest SET destfile = '".$hexnumber."' WHERE pkey = ".$args[0]);
// $query = ("UPDATE blobtest SET destfile = '000000002257' WHERE pkey = ".$args[0]);
// Above produces: \x303030303030303032323537
// (makes sense; it's quoted as a string)
// $query = ("UPDATE blobtest SET destfile = 000000002257 WHERE pkey = ".$args[0]);
// Above produces: ERROR: column "destfile" is of type bytea but expression is of type integer
// $query = ("UPDATE blobtest SET destfile = '8791' WHERE pkey = ".$args[0]);
// Above produces: \x38373931 as expected...
/ $query = ("UPDATE blobtest SET destfile = 8791 WHERE pkey = ".$args[0]);
// Above produces: ERROR: column "destfile" is of type bytea but expression is of type integer
// $query = ("UPDATE blobtest SET destfile = '"."'".$packed_hex."'"."' WHERE pkey = ".$args[0]);
// Above produces: \x only...
$query = ("UPDATE blobtest SET destfile = "."'".$packed_hex."'"." WHERE pkey = ".$args[0]);
// unterminated quoted string at or near "'"
【问题讨论】:
标签: php postgresql