Non-English characters are displayed as question marks on my php page - displayed in the database

I have a MySQL database table populated with non-English data. When I view the data in the Navicat MySQL browser, the data looks great. However, when I run a php script to select and display data on a web page, question marks are displayed instead. The page encoding is set to utf8, and even MySQL mapping is set to utf8 - something doesn’t work when selecting and displaying ... please help.

+3
source share
2 answers

MySQL connection settings may be corrupted. Run this MySQL command when connecting to the database with PHP before running any other SQL commands:

SET names 'utf8';

This should set the connection encoding to UTF-8. As you say, the page and database are already in UTF-8 (this also means that the page is sending Content-Type: text/html; charset=utf-8); the connection itself may accidentally have a different encoding by default: (

+5
source

In addition, in the HTML inside, inside we have to write:

<meta charset="utf-8">

When we create the MySQL database, we should use something like this:

CREATE DATABASE `base` DEFAULT CHARACTER SET=utf8;

When we create tables, use:

CREATE TABLE `base`.`table` (
`key` int(5) unsigned NOT NULL,
`name` varchar(100),
...
PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

In the PHP code, after mysql_connect () and mysql_select_db (), use:

mysql_query('SET NAMES UTF8');
+3
source

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


All Articles