Home  >  Article  >  Backend Development  >  php连接access数据库编码问题

php连接access数据库编码问题

WBOY
WBOYOriginal
2016-06-23 14:16:461056browse

php连接access数据库,编码为utf-8时输出报错,用gbk正常。
可是我现在必须要用utf-8,请问怎么办?数据库还是用access!

$gets=$_GET['sid'];$connstr="DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath("music.mdb");$conn=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );header("Content-type:text/xml"); echo '<?xml version="1.0" encoding="gbk" ?>';echo '<veryhdmtvlist>';$sql = "select * from mtv_music where sid=$gets order by musicid";$rs = odbc_do($conn,$sql);echo '<pageinfo recordcount="160" pagecount="10" pagesize="16" pageindex="1"/>';echo '<mtvlist>';while(odbc_fetch_array($rs)){		$field_0=odbc_result($rs,"musicid");$field_1=odbc_result($rs,"sid");$field_2=odbc_result($rs,"musicname");echo "<mtv mtvid='$field_0' sid='$field_1' singer='' name='$field_2' pic='' link='http://192.168.253.34:102/play.php?vid=$field_0'/>";}odbc_close($conn);echo '</mtvlist>';echo '</veryhdmtvlist>';


回复讨论(解决方案)

不明白为什么一定要用 utf-8 编码,自找麻烦?
你有 echo '';
连输出都是 gbk 的,那么 utf-8 的作用体现在那呢?

嗯,如果你需要输出 utf-8 的 xml 就这样写

....iconv_set_encoding("internal_encoding", "GBK"); //这是新加的iconv_set_encoding("output_encoding", "UTF-8"); //这是新加的ob_start("ob_iconv_handler"); //这是新加的header("Content-type:text/xml"); echo '<?xml version="1.0" encoding="utf-8" ?>'; //这里改成 utf-8 字符集echo '<veryhdmtvlist>'; $sql = "select * from mtv_music where sid=$gets order by musicid";$rs = odbc_do($conn,$sql);echo '<pageinfo recordcount="160" pagecount="10" pagesize="16" pageindex="1"/>';echo '<mtvlist>'; while(odbc_fetch_array($rs)){        $field_0=odbc_result($rs,"musicid");$field_1=odbc_result($rs,"sid");$field_2=odbc_result($rs,"musicname");echo "<mtv mtvid='$field_0' sid='$field_1' singer='' name='$field_2' pic='' link='http://192.168.253.34:102/play.php?vid=$field_0'/>";}odbc_close($conn);echo '</mtvlist>';echo '</veryhdmtvlist>';

编码一致,才不会出现问题。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php验证显示内容Next article:PHP发送微博失败