【问题标题】:How to make the database connection to be utf-8 compliant in this setting?如何在此设置中使数据库连接符合 utf-8 标准?
【发布时间】:2015-08-29 13:29:09
【问题描述】:

这是我的 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

【问题讨论】:

    标签: phalcon


    【解决方案1】:

    也将编码参数添加到 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
    

    【讨论】:

    • 我添加了选项,但数据表没有显示重音字母!那为什么呢?
    • 您是否设置了链接接受的答案中指出的所有内容:即 DB 的排序规则?
    • 试试utf8_general_ci
    • 与 utf8_general_ci 的结果相同。
    • 那么究竟是什么问题,我看不到您提供的问题?从数据库输出字符?还有什么?有任何错误信息吗?
    【解决方案2】:

    好的,我找到了解决方案:我修改了 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;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-03-05
      • 2014-07-08
      • 2013-08-07
      • 1970-01-01
      • 2013-09-14
      • 2011-06-11
      • 2021-06-15
      • 2014-03-18
      • 1970-01-01
      相关资源
      最近更新 更多