I do not work in php, but I needed it.

enter image description here enter image description here

For some reason, the name of objects is distorted, what is the problem and how to fix it ???

<?php header("Content-Type: text/html; charset=utf-8"); $servername = "**********"; $username = "********"; $password = "*******"; $dbname = "********"; function connect(){ $conn = mysqli_connect("*******", "********", "********", "********"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } return $conn; } function init(){ //вывожу список товаров $conn = connect(); $sql = "SELECT id, name FROM goods"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { $out = array(); while($row = mysqli_fetch_assoc($result)) { $out[$row["id"]] = $row; } echo json_encode($out); } else { echo "0"; } mysqli_close($conn); } ?> <?php header("Content-Type: text/html; charset=utf-8"); $mysqli->set_charset("utf8"); //читать json файл $json = file_get_contents( '../goods.json'); $json = json_decode($json, true); //письмо $message = ''; $message .= '<h1>Заказ в магазине</h1>'; $message .='<p>Телефон: '.$_POST['ephone'].'</p>'; $message .='<p>Почта: '.$_POST['email'].'</p>'; $message .='<p>Клиент: '.$_POST['ename'].'</p>'; $cart = $_POST['cart']; $sum = 0; foreach ($cart as $id=>$count) { $message .=$json[$id]['name'].' --- '; $message .=$count.' --- '; $message .=$count*$json[$id]['cost']; $message .='<br>'; $sum = $sum + $count*$json[$id]['cost']; } $message .='Всего: '.$sum; //print_r($message); $to = '******************'.','; $to .=$_POST['email']; $specttext = '<!DOCTYPE html><html><head><title>Заказ</title></head><body>'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; $m = mail($to, 'Заказ в магазине', $specttext.$message.'</body></html>', $headers); if ($m) {echo 1;} else {echo 0;} ?> 
  • The encoding is bad. - Manitikyl
  • @Manitikyl like utf-8 stands everywhere - Rafael Shepard
  • @korytoff understood) will now) - Rafael Shepard

1 answer 1

If when working with mysql charset utf8 is specified then try to add at the beginning of the file

 header('Content-Type: text/html; charset=utf-8'); 

You can still through the file htaccess

 AddDefaultCharset UTF-8 

When working with a database, if you work through mysql

 mysql_connect("localhost","user","password"); mysql_select_db("db"); mysql_set_charset("utf8"); 

If through mysqli

 $conn = mysqli_connect($servername, $username, $password, $dbname); mysqli_set_charset($conn,"utf8"); 

If PDO

 $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $pdo = new PDO($dsn, $user, $pass, $opt); 

UPD: In your case

 function connect(){ $conn = mysqli_connect("*******", "********", "********", "********"); mysqli_set_charset($conn,"utf8"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } return $conn; } 
  • The first option is already registered everywhere) 2nd - where can I register? 3 option also do not know where. 4 option is registered) - Rafael Shepard
  • one
    Completed. I think you will understand. If not then throw a piece of code here. - VikiMayson
  • thanks a lot) - Rafael Shepard
  • Thanks, it worked! - Rafael Shepard