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

WBOY
Release: 2016-06-13 12:03:58
Original
1015 people have browsed it

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

function writeStatistic($sql,$txt_name){<br />	$filename="".$txt_name;//注意修改文件的路径<br />	if (file_exists($filename)) {<br />		$fp=fopen($filename,"a+");<br />	}else{<br />		$fp=fopen($filename,"w+");<br />	}<br /><br />	$text=$sql."\r\n";<br />	fwrite($fp,$text);<br />	fclose($fp);<br />}<br />/**<br />* 向指定文件写入字符集设置句柄<br />*<br />* 参数: string $txt_name : 指定文件名<br />* 返回: void<br />* 作用域: public<br />*/<br />function writeStatistic_set_name($txt_name){<br />    $set_name="<?xml version=\"1.0\" encoding=\"utf-8\"?><br /><urlset>";<br />	//转化为utf-8编码<br />	<br />	$filename="".$txt_name;//注意修改文件的路径<br />	if (file_exists($filename)) {<br />		unlink($filename);<br />		$fp=fopen($filename,"a+");<br />	}else{<br />		$fp=fopen($filename,"w+");<br />	}<br /><br />	$text=$set_name."\r\n";<br />	fwrite($fp,$text);<br />	fclose($fp);<br />}<br />$nowDate = date("Y-m-d");<br />$txt_name='sitemap.xml';<br />writeStatistic_set_name($txt_name);<br />$sql="select * from jieqi_article_article ORDER BY lastupdate DESC";<br />$db->query($sql);<br />for($i=1;$i<= $db->num_rows();$i++){<br />	$db->next_record();<br />	$siteid = $db->f(siteid);<br />	$haha = _;<br />	$articleid = $db->f(articleid);<br />	$newname = $db->f(articlename);<br />	$newid = $db->f(lastchapterid);<br />	$nowDate = $db ->f(lastupdate);<br />	$nowDate = date("Y-m-d H:m:s",$nowDate);<br />	$url = "<br />	<url><br />	<loc>http://www.1.org/html/$articleid/$newid.html </loc><br />	<lastmod>$nowDate</lastmod><br />	<changefreq>always</changefreq><br />	<priority>1.0</priority><br />	<name>$newname</name><br />	</url>";<br />	//转化为utf-8编码<br />	$url = utf8_encode($url);<br />	writeStatistic($url,$txt_name);<br />}<br />$db->free();<br />$over='</urlset>';<br />writeStatistic($over,$txt_name);<br />print "Google Sitemap生成结束!<br/><a href='sitemap.xml' target='_blank'>点击这里查看</a>";<br />exit;<br />?>
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