今天我正在开发基于
PHP,MysqL和Zend Framework的应用程序时出错.此外,我使用
phpseclib使用
AES algorithm加密数据,这里出现了问题. AES算法的输出是看起来MysqL不喜欢的形式.事实上,当我尝试将数据插入数据库时,得到一个sql Exception.错误是:
sqlSTATE[HY000]: General error: 1366 Incorrect string value: '\xE4\xD5\xABtZM...' for column 'Name'
我已经阅读了Stackoverflow中发布的所有答案,并且还将Googled的问题,但所有提出的解决方案已经在我的代码.数据库,表和所有col都有排序规则utf8_general_ci.下面你可以看到相关代码:
> Application.ini来看看如何设置连接
> Database.PHP看看我如何检索数据库连接
> Model.PHP看看我如何尝试在数据库中插入数据
> encrypt()来查看我如何使用AES类加密数据
>表定义(如果知道所有都在utf8中是不够的)
的application.ini
resources.db.adapter = "Pdo_MysqL" resources.db.params.charset = "utf8" resources.db.params.host = "localhost" resources.db.params.username = "********" resources.db.params.password = "********" resources.db.params.dbname = "dbname"
为database.PHP
public static function getDb() { if (self::$Db === NULL) self::$Db = Zend_Db_Table::getDefaultAdapter(); return self::$Db; }
model.PHP
$Values = array( 'Id' => $this->Id,'Name' => $this->Name,'CreationDate' => $this->CreationDate,); $RowChanged = $Db->insert('TABLENAME',$Values);
加密()
public static function encrypt($Data,$EncryptionKey) { $AES = new Crypt_AES(); $AES->setKey($EncryptionKey); return $AES->encrypt($Data); }
表
CREATE TABLE IF NOT EXISTS `table` ( `Id` mediumint(8) unsigned NOT NULL,`Name` varchar(200) DEFAULT NULL,`CreationDate` date NOT NULL,PRIMARY KEY (`Id`),) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我意识到这是一个用于MysqL的AES_ENCRYPT的
reference,但是看起来您可能需要将varchar(200)更改为varbinary(200)(或更大),因为AES似乎返回一个二进制字符串.
原文链接:https://www.f2er.com/php/131548.html在MysqL站点上有一个不太清楚的explanation.
Many encryption and compression functions return strings for which the result might contain arbitrary byte values. If you want to store these results,use a column with a VARBINARY or BLOB binary string data type. This will avoid potential problems with trailing space removal or character set conversion that would change data values,such as may occur if you use a nonbinary string data type (CHAR,VARCHAR,TEXT).