使用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"

查看:48
本文介绍了使用PHP将openssl_random_pseudo_bytes的输出存储在Postgres中吗?我收到一个错误:用于编码&"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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆