Home >Backend Development >PHP Problem >What to do if the foreach code in php is garbled

What to do if the foreach code in php is garbled

藏色散人
藏色散人Original
2021-02-23 10:21:502253browse

Solution to php's foreach garbled code: 1. Change the encoding format from the browser to utf-8; 2. Specify the encoding format in the program, the code is "header('Content-Type: text/ html; charset="utf-8"')".

What to do if the foreach code in php is garbled

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

Specific question:

php foreach loop data is all garbled, how to solve it?

This is the code

<?php
$link = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_query("set names utf-8;");
mysql_query("SET character_set_connection=&#39;utf-8&#39;, character_set_results=&#39;utf-8&#39;, character_set_client=&#39;utf-8&#39;");
$sql="select * from baoming.baimingbiao";
$aa=mysql_query($sql);
$v = mysql_fetch_array($aa);
if(is_array($v))
{
foreach($v as $k=>$r)
{
$aaa .="<table>";
$aaa.="<tr><td>".$r[&#39;name&#39;]."</td><td>".$r[&#39;dianhua&#39;]."</td><td>".$r[&#39;xingbie&#39;]."</td><td>".$r[&#39;youxiang&#39;]."</td><td>".$r[&#39;danwei&#39;]."</td><td>".$r[&#39;zhiye&#39;]."</td><td>".$r[&#39;hjhhy&#39;]."</td><td>".$r[&#39;zhusu&#39;]."</td><td>".$r[&#39;shouji&#39;]."</td><td>".$r[&#39;yjje&#39;]."</td><td>".$r[&#39;yfje&#39;]."</td><td>".$r[&#39;ddbz&#39;]."</td><td>".$r[&#39;fjbz&#39;]."</td><td>".$r[&#39;ddbz&#39;]."</td><td>".$r[&#39;fjbz&#39;]."</td><td>".$r[&#39;beizhu&#39;]."</td><td>".$r[&#39;yewuyuan&#39;]."</td><td>".$r[&#39;fjh&#39;]."</td></tr>";
}
$aaa .="</table>";
echo $aaa;
exit;
$handle = @fopen(&#39;./c.xls&#39;, &#39;w&#39;);
if($handle)
{
if(@fwrite($handle, $aaa))
{
echo &#39;1&#39;;
}
else
{
echo "0";
}
fclose($handle);
@header("location:xls_bu.php");
}
}
?>

Solution:

Change the encoding format from the browser to utf-8 and see if If it is not garbled, just specify the encoding format in the program.

header(&#39;Content-Type: text/html; charset="utf-8"&#39;)

Recommended: "PHP Video Tutorial"

The above is the detailed content of What to do if the foreach code in php is garbled. For more information, please follow other related articles on the PHP Chinese website!

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:What does == mean in phpNext article:What does == mean in php