使用PHP+Smarty+Mysql  编程时,PHP页面和Smarty模板页面,包括数据库都是都是UTF-8的,可输出的中文数据就是乱码,请高人指点。

解决方案 »

  1.   

    index.php<?php
    require_once("config.php");
    mysql_connect("localhost","root","root");
    mysql_select_db("news");
    mysql_query("set names GB2312");
    $sql="select * from news";
    $result=mysql_query($sql);
    $row=mysql_fetch_array($result);
    $s->assign("news",$row[title]);
    $s->display("index.tpl");?>
      

  2.   

    config.php
    <?php
    $arr=$_SERVER['DOCUMENT_ROOT'];
    define("ROOT",$arr."/s/");
    require_once(ROOT."smarty/smarty.class.php");
    $s = new smarty();
    $s->template_dir=ROOT."templates";  //模板文件夹
    $s->compile_dir =ROOT."templates_c";  //编译过的模板文件夹
    $s->left_delimiter="<{";  //开始定界符
    $s->right_delimiter="}>";  //结束定界符
    ?>
      

  3.   

    index.tpl<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    </head>
    <body>
    <{$news}>
    </body>
    </html>
      

  4.   

    mysql_query("set names GB2312");
    你这不是GB2312吗?
      

  5.   

    忘了,那是后改的,原来是UTF-8的
      

  6.   

    没有乱码,都是正常的,数据库,表、title字段,都是utf8_general_ci编码的
      

  7.   

    header("content-type:text/html; charset=utf-8");
    在处理页面加上试一下
      

  8.   

    editplus转换编码也好麻烦,dw在编码转换上倒是挺方便的,设定下header,然后再在记事本打开文件,看下文件当前是什么编码。
      

  9.   

    你试一下改成:mysql_query("set names utf8");
    不需要-的