title: Mysql: General error: 1366 Incorrect string value
body: <p>Today I got an error while I was developing an app based on PHP, MySql and the Zend Framework. Moreover, I'm using <a href="http://phpseclib.sourceforge.net/" rel="noreferrer">phpseclib</a> to encrypt the data using the <a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard" rel="noreferrer">AES algorithm</a> and here came the problem. The output of the AES algorithm is in a form that seems MySql doesn't like. Infact when I try to insert the data into the database a got an Sql Exception. The error is:</p>
  
  <pre><code>SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE4\xD5\xABtZM...' for column 'Name'
  </code></pre>
  
  <p>I've already read all the answers posted on Stackoverflow and have also Googled the problem but all the proposed solution were already in my code. Database, tables and all the cols have Collation <code>utf8_general_ci</code>. Below you can see the relevant code:</p>
  
  <ol>
  <li>Application.ini to see how is set up the connection</li>
  <li>Database.php to see how I retrieve the database connection</li>
  <li>Model.php to see how I try to insert the data in the database</li>
  <li>encrypt() to see how I use the AES class to encrypt the data</li>
  <li>Table definition (If know that all are in utf8 isn't enough)</li>
  </ol>
  
  <p><strong>application.ini</strong></p>
  
  <pre><code>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"
  </code></pre>
  
  <p><strong>database.php</strong></p>
  
  <pre><code>public static function getDb()
  {
     if (self::$Db === NULL)
        self::$Db = Zend_Db_Table::getDefaultAdapter();
     return self::$Db;
  }
  </code></pre>
  
  <p><strong>model.php</strong></p>
  
  <pre><code>$Values = array(
     'Id' =&gt; $this-&gt;Id,
     'Name' =&gt; $this-&gt;Name,
     'CreationDate' =&gt; $this-&gt;CreationDate,
  );
  $RowChanged = $Db-&gt;insert('TABLENAME', $Values);
  </code></pre>
  
  <p><strong>encrypt()</strong></p>
  
  <pre><code>public static function encrypt($Data, $EncryptionKey)
  {
     $AES = new Crypt_AES();
     $AES-&gt;setKey($EncryptionKey);
     return $AES-&gt;encrypt($Data);
  }
  </code></pre>
  
  <p><strong>table</strong></p>
  
  <pre><code>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;
  </code></pre>
  
  <p><strong>Question:</strong> How can I solve the problem and store the data into the database?</p>