且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP将openssl_random_pseudo_bytes的输出存储在Postgres中?我收到一个错误:编码“ UTF8”的字节序列无效。

更新时间:2023-11-25 08:42:34

openssl_random_pseudo_bytes 函数输出二进制数据,而我试图将输出存储在字符类型不同的数据库字段中,错误地将其视为

The openssl_random_pseudo_bytes function outputs binary data, and I was trying to store the output in a database field of data type character varying, mistakenly treating it like a string.

bytea 是PostgreSQL数据库中字段的正确数据类型。

bytea is the correct data type for the field in the PostgreSQL database.

http://www.postgresql.org/docs/9.2/interactive/datatype-binary.html

更新:另外,看来您需要在输出上使用 pg_escape_bytea 才能将二进制数据实际插入数据库中。

Update: Also, it seems you need to use pg_escape_bytea on the output to be able to actually insert the binary data into the database.

感谢注释中的减少 lechlukasz VolkerK