在此设置中如何使数据库连接符合 utf-8 标准?

How to make the database connection to be utf-8 compliant in this setting?

这是我的index.php文件(数据库是MySQL):

<?php

use Phalcon\Loader;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

include("../app/config_const.php");
include("../app/config_inc.php");
include("../app/lib/PSR.php");
try {

    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/'
    ))->register();

    // Create a DI
    $di = new FactoryDefault();

    // Setup the database service
    $di->set('db', function(){
        $cnx = new DbAdapter(array(
            "host"     => "localhost",
            "username" => "root",
            "password" => "",
            "dbname"   => BDD
        ));
        return $cnx;
    });

    // Setup the view component
    $di->set('view', function(){
        $view = new View();
        $view->setViewsDir('../app/views/');

        $view->registerEngines(array(
            ".phtml" => 'Phalcon\Mvc\View\Engine\Volt'
        ));
        return $view;
    });

    // Setup a base URI so that all generated URIs include the "phalcon" folder
    $di->set('url', function(){
        $url = new UrlProvider();
        $url->setBaseUri('/resto/');
        return $url;
    });

    //Register the flash service with custom CSS classes
    $di->set('flash', function(){
        $flash = new \Phalcon\Flash\Direct(array(
            'error' => 'alert alert-error',
            'success' => 'alert alert-success',
            'notice' => 'alert alert-info',
        ));
        return $flash;
    });

    //Handle the request
    $app = new Application($di);

   echo $app->handle()->getContent();

} catch(\Exception $e) {
     echo "Exception:  ", $e->getMessage();
}
?>

在这种情况下如何使数据库设置 utf-8 兼容?

同时向 DB 适配器添加编码参数:

// Setup the database service
$di->set('db', function(){
    $cnx = new DbAdapter(array(
        "host"     => "localhost",
        "username" => "root",
        "password" => "",
        "dbname"   => BDD,
        "charset"  => "utf8",
        "options" => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        ),
    ));
    return $cnx;
});

Reference.

您还可以为 .htaccess 文件设置编码:

# your file could start with something like this
RewriteEngine On
RewriteBase /
# add next line
AddDefaultCharset utf-8

好的,我找到了解决方案:我修改了服务器处理的 ssp.class.php:

static function sql_connect($sql_details) {
        try {
            $db = @new PDO("mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
                $sql_details['user'],$sql_details['pass'],array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
            $db->exec("set names utf8");
            $db->exec("set character_set_client='utf8'");
            $db->exec("set character_set_results='utf8'");
            $db->exec("set collation_connection='utf8_bin'");
        }
        catch (PDOException $e) {
            self::fatal("An error occurred while connecting to the database. ".
                "The error reported by the server was: ".$e->getMessage());
        }
        return $db;
    }