PHP MySQL查询导致乱码

时间:2014-04-07 18:50:46

标签: php mysql

我正在用PDO进行MySQL查询。 我正在使用charset=utf8。 查询找到答案(应该是全名)。 但是,结果总是如下所示:

  

=?UTF-8 2 B 4 somejibber =

任何想法可能出错?

谢谢!

修改

以下是我正在做的事情: 我正在使用fmbiete的Z-Push-Contrib从我的ispconfig3数据库中获取dovecot用户的全名。

https://github.com/fmbiete/Z-Push-contrib/blob/master/backend/imap/config.php https://github.com/fmbiete/Z-Push-contrib/blob/master/backend/imap/imap.php

define('IMAP_FROM_SQL_DSN', 'mysql:host=localhost;dbname=dbispconfig;charset=utf8');
define('IMAP_FROM_SQL_USER', 'z-push');
define('IMAP_FROM_SQL_PASSWORD', 'XXXYYY');
define('IMAP_FROM_SQL_OPTIONS', serialize(array(PDO::ATTR_PERSISTENT => true)));
define('IMAP_FROM_SQL_QUERY', 'select name, email from mail_user where login = "#username"');
define('IMAP_FROM_SQL_FIELDS', serialize(array('name', 'email')));
define('IMAP_FROM_SQL_FROM', '#name <#email>');

_

private function getFromSql($username, $domain) {
        $from = $username;

        $dbh = $sth = $record = null;
        try {
            $dbh = new PDO(IMAP_FROM_SQL_DSN, IMAP_FROM_SQL_USER, IMAP_FROM_SQL_PASSWORD, unserialize(IMAP_FROM_SQL_OPTIONS));
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getFromSql() - Connected to SQL Database"));

            $sql = str_replace('#username', $username, str_replace('#domain', $domain, IMAP_FROM_SQL_QUERY));
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getFromSql() - Searching From with filter: %s", $sql));
            $sth = $dbh->prepare($sql);
            $sth->execute();
            $record = $sth->fetch(PDO::FETCH_ASSOC);
            if ($record) {
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getFromSql() - Found entry in SQL Database. Generating From"));
                $from = IMAP_FROM_SQL_FROM;
                foreach (unserialize(IMAP_FROM_SQL_FIELDS) as $field) {
                    $from = str_replace('#'.$field, $record[$field], $from);
                }
                $from = $this->encodeFrom($from);
            }
            else {
                ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendIMAP->getFromSql() - No entry found in SQL Database"));
            }
        }
        catch(PDOException $ex) {
            ZLog::Write(LOGLEVEL_WARN, sprintf("BackendIMAP->getFromSql() - Error getting From value from SQL Database: %s", $ex));
        }

        $dbh = $sth = $record = null;

        return $from;
    }

修改

正如@ rocket-hazmat在评论中指出的那样,这似乎是预期的行为而且很好 - 这是一个无用的线程; - )

3 个答案:

答案 0 :(得分:2)

=?utf-8?B?gibberish?=表示有人将信息存储为BASE64 UTF8 ENCODED。

这在电子邮件系统中很常见,其中主题和发件人:字段可能不是低ASCII,甚至根本不是任何西方编码。

因此,如果要恢复信息,则需要解析该值。

例如:

$gibberish = '=?utf-8?B?UmVuw6llIERldXhpw6htZQo=?=';

if (preg_match('#=\\?utf-8\\?B\\?(.*)\\?=#', $gibberish, $gregs)) {
    $clean = base64_decode($gregs[1]);
    // You may need to utf_decode or iconv the value to suit your needs.
} else {
    // Do not clean :-(
    $clean = $gibberish;
}

print $clean;

输出

Renée Deuxième

作为附注,另一种可能的编码是Quoted-Printable UTF8,它可以通过前缀=?utf-8?Q识别(注意Q而不是B)。

答案 1 :(得分:-1)

这里没有错,也没有错。

=?UTF-8?B?somejibber?=是存储在数据库中的完全有效的值。你可能会问他们为什么这样做。我们不能告诉你。

答案 2 :(得分:-1)

尝试将名称设为utf-8

$example->exec("set names utf8");