请问这个PHP调用的MYSQL数据为什么乱码了?
程序员文章站
2024-01-04 09:29:16
...
function writeStatistic($sql,$txt_name){ $filename="".$txt_name;//注意修改文件的路径 if (file_exists($filename)) { $fp=fopen($filename,"a+"); }else{ $fp=fopen($filename,"w+"); } $text=$sql."\r\n"; fwrite($fp,$text); fclose($fp);}/*** 向指定文件写入字符集设置句柄** 参数: string $txt_name : 指定文件名* 返回: void* 作用域: public*/function writeStatistic_set_name($txt_name){ $set_name=""; //转化为utf-8编码 $filename="".$txt_name;//注意修改文件的路径 if (file_exists($filename)) { unlink($filename); $fp=fopen($filename,"a+"); }else{ $fp=fopen($filename,"w+"); } $text=$set_name."\r\n"; fwrite($fp,$text); fclose($fp);}$nowDate = date("Y-m-d");$txt_name='sitemap.xml';writeStatistic_set_name($txt_name);$sql="select * from jieqi_article_article ORDER BY lastupdate DESC";$db->query($sql);for($i=1;$inum_rows();$i++){ $db->next_record(); $siteid = $db->f(siteid); $haha = _; $articleid = $db->f(articleid); $newname = $db->f(articlename); $newid = $db->f(lastchapterid); $nowDate = $db ->f(lastupdate); $nowDate = date("Y-m-d H:m:s",$nowDate); $url = " ';writeStatistic($over,$txt_name);print "Google Sitemap生成结束!"; //转化为utf-8编码 $url = utf8_encode($url); writeStatistic($url,$txt_name);}$db->free();$over=' http://www.1.org/html/$articleid/$newid.html $nowDate always 1.0 $newname
点击这里查看";exit;?>
生成后的结果是这样。。
请问要怎么才能正确调用出中文字啊?
回复讨论(解决方案)
你用错了函数!
$url = utf8_encode($url);
要改为
$url = iconv(;gbk', 'utf-8', $url);
当然 $url 中的数据编码要对
其实你的 $db 设置字符集为 utf8,就不需要转码了