Home > Article > Backend Development > What to do if the foreach code in php is garbled
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"')".
#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='utf-8', character_set_results='utf-8', character_set_client='utf-8'"); $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['name']."</td><td>".$r['dianhua']."</td><td>".$r['xingbie']."</td><td>".$r['youxiang']."</td><td>".$r['danwei']."</td><td>".$r['zhiye']."</td><td>".$r['hjhhy']."</td><td>".$r['zhusu']."</td><td>".$r['shouji']."</td><td>".$r['yjje']."</td><td>".$r['yfje']."</td><td>".$r['ddbz']."</td><td>".$r['fjbz']."</td><td>".$r['ddbz']."</td><td>".$r['fjbz']."</td><td>".$r['beizhu']."</td><td>".$r['yewuyuan']."</td><td>".$r['fjh']."</td></tr>"; } $aaa .="</table>"; echo $aaa; exit; $handle = @fopen('./c.xls', 'w'); if($handle) { if(@fwrite($handle, $aaa)) { echo '1'; } 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('Content-Type: text/html; charset="utf-8"')
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!