S
S
skvoshiz2015-07-24 23:17:57
PHP
skvoshiz, 2015-07-24 23:17:57

Why does PHP encoding change?

Hello, I ran into a problem that is not clear to me:
I am making a POST request to the server to the test.php file with two parameters: info1, info2
info1 is data for MySQL
info2 is just a string with Russian letters = "Hello"
The test.php file itself

<?php
require_once('data.php');
require 'main_info.php';
$site= $_POST['info1']; //site
$var1 = $_POST['info2']; //var1
if (!empty($site))
{	
  $link = mysql_connect($MySQL_hostname,$MySQL_username,$MySQL_password);
  mysql_select_db($MySQL_databasename);
  $query = "SELECT * FROM $MySQL_table  WHERE `Site` = '$site'"; 
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  $var2 = $row['Title'];
  echo $var1 .";" .$var2;
}
else
{
  echo "ERR";
}
?>

$ row['Title'] - in the table this cell is occupied by the text "Hey" And as
a result, my answer from this file goes like this: .$var2; will just echo $var1; then the answer is: Hi What's the problem here? How to make the response come in normal encoding, or what is it all about?



Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-07-25
@skvoshiz

The problem is obviously that you have two encodings mixed up. The browser usually tries to determine the encoding automatically, so in the second case UTF-8 is correctly detected (where "Hello"), and in the first case, another encoding pulls the blanket over itself (probably 1251), and characters encoded in UTF-8 are displayed crookedly . The string "Hello" comes to you in the encoding that is set for the current connection to the database, and it, apparently, is not UTF-8. Put down utf8, for example, in this way. Or change the default encoding in mysqld settings.
By the way, switch to PDO if you are learning data access in PHP. this method is considered obsolete.

A
Alex, 2015-07-24
@isqua

In what encoding is the page given to the user? I suspect that the browser sends data in the same encoding as it receives. What is the encoding in the database? What is in the PHP settings? I recommend setting UTF-8 everywhere, then there will be no problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question