Cyrillic characters in INSERT request

$dblocation = "localhost"; $dbname = "xx"; $dbuser = "xx"; $dbpasswd = "xx"; $dbcnx = @mysql_connect($dblocation,$dbuser,$dbpasswd); mysql_select_db($dbname, $dbcnx); mysql_query("SET NAMES utf8"); mysql_query("SET COLLATION_CONNECTION=utf8_bin"); $name = mysql_real_escape_string($name); $about = mysql_real_escape_string($about); mysql_query("INSERT INTO votes (name, about, active) VALUES('".$name."', '".$about."', 1 ) ") or die(mysql_error()); 

It works great when $ name and $ about are not Cyrillic. But when it is Cyrillic - the script simply adds a line with an empty name and fields. What to do?
DB - UTF-8, manually adding lines with phpMyAdmin with Cyrillic characters works fine, PHP-script is UTF-8, all UTF-8.

+6
source share
3 answers

You must have:

1. header ("Content-Type: text/html; charset=utf-8"); or <meta http-equiv="content-type" content="text/html; charset=utf-8" />

2.mysql_query ("SET NAMES 'utf8'");

3. Set the encoding of your file to utf-8 without specification

+3
source

Specify the character set to be used by the connection to mysql_set_charset(...') . See http://php.net/manual/en/function.mysql-set-charset.php .

0
source

I figured out a lot of the Cyrillic alphabet, and it's really complicated. So far in my experience, I have not used any COLLATION_CONNECTION. Everything that I do is put, like you, all possible files in utf8. Then I put the fields in the database into "utf8_unicode_ci", and I use only SET NAMES parameters to set the encoding of the connection. That should be enough. Keep in mind that Set names can be complex since there is only one place in the extra and it will not work. I use:

 define("DBENCODING", "utf8"); mysql_query("SET NAMES '".DBENCODING."'", $this->connection); 

I hope I was able to help.

0
source

Source: https://habr.com/ru/post/889737/


All Articles