使用PHP将openssl_random_pseudo_bytes的输出存储在Postgres中?我收到一个错误:编码“ UTF8”的字节序列无效。 [英] Storing the output of openssl_random_pseudo_bytes in Postgres using php? I get an error: invalid byte sequence for encoding "UTF8"
问题描述
我正在使用php中的 openssl_random_pseudo_bytes
函数生成盐。但是,我无法将输出存储到PostgreSQL数据库中。
I'm generating a salt using the openssl_random_pseudo_bytes
function in php. However, I am unable to store the output into a PostgreSQL database.
我收到一个错误:
错误:用于编码 UTF8的无效字节序列
ERROR: invalid byte sequence for encoding "UTF8"
现在,我意识到所讨论的数据库的编码是设置为UTF8,并且该函数的输出不匹配,但是解决此问题的正确方法是什么?
Now I realize the encoding of the database in question is set to UTF8, and that the output of the function doesn't match, but what is the correct way of solving this?
这是一种随机盐,我担心通过任何过程将其转换为UTF8都将使其不再具有密码安全性吗?
推荐答案
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 。
这篇关于使用PHP将openssl_random_pseudo_bytes的输出存储在Postgres中?我收到一个错误:编码“ UTF8”的字节序列无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!