请问这个PHP调用的MYSQL数据为什么乱码了?

WBOY
Release: 2016-06-23 13:52:24
Original
832 people have browsed it

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="<?xml version=\"1.0\" encoding=\"utf-8\"?><urlset>";	//转化为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;$i<= $db->num_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 = "	<url>	<loc>http://www.1.org/html/$articleid/$newid.html </loc>	<lastmod>$nowDate</lastmod>	<changefreq>always</changefreq>	<priority>1.0</priority>	<name>$newname</name>	</url>";	//转化为utf-8编码	$url = utf8_encode($url);	writeStatistic($url,$txt_name);}$db->free();$over='</urlset>';writeStatistic($over,$txt_name);print "Google Sitemap生成结束!<br/><a href='sitemap.xml' target='_blank'>点击这里查看</a>";exit;?>
Copy after login


生成后的结果是这样。。


请问要怎么才能正确调用出中文字啊?


回复讨论(解决方案)

你用错了函数!
$url = utf8_encode($url);
要改为
$url = iconv(;gbk', 'utf-8', $url);

当然 $url 中的数据编码要对

其实你的 $db 设置字符集为 utf8,就不需要转码了

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template