GET A FREE QUOTE

How to solve multi-language issue using MySQL and PHP

Often times we find an issue in displaying text or string of another language like Chinese, Urdu or any other international language from the database to webpage. It might display some special character like “???? ???????????? ?????????? ???????” or other instead of original language string or text.

Due to UTF-8 not used properly this type of error may accrue. There are many different ways to assign character set UTF-8 as below:

• Set in the php.ini file
• Set in Header using the meta tag
• Use htmlspecialchars() function
• Set attribute in XML
• Set in MySQL connections

Option 1: – Set in the php.ini File:

First check default charset value set UTF-8 in phpinfo. If not found or default_charset value is not set ‘UTF-8’, then need to change in php.ini file. In php.ini if value exist with other value change to ‘UTF-8’ or if not exist add default_charset = “UTF-8”. Sometimes, default_charset value is disabled in file, in that case remove # from front to enable.

Option 2: – Set in Header using the meta tag:

Set on web header charset value as below. Use the header() function before generating any content as below:

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

Set in meta-tag in the header:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Option 3: – Use htmlspecialchars() function

The prior version of PHP 5.4.0 default charset is ISO-8859-1. From PHP 5.4.0 default value is UTF-8. In any case, if default charset is not UTF-8 and you want to use UTF-8 for some string or text, use htmlspecialchars function. If string or text not displayed properly on webpage and you want to convert some predefined characters to HTML entities, then use below mentioned function:

htmlspecialchars($str, ENT_NOQUOTES, "UTF-8");

Option 4:- Set attribute in xml

XHTML parsers do not recognize the encoding declarations in meta tags. For that need XML declaration only as below:

<?xml version="1.0" encoding="utf-8"?>

Option 5:- Set in MySQL connections

Often found that after setting all charset in the page we found the issue in some string/text which is from SQL server. For the issue in MySQL, set default character for all MySQL connections.

$conn = mysql_connect('localhost', 'user', 'password'); 
	mysql_set_charset('utf8', $conn);

For PHP 5.5.0 mysqli connections:

$conn = new mysqli('localhost', 'user', 'password', 'database'); 
	if (mysqli_connect_errno()) {
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}
	/* change character set to utf8 */
  	if (!$mysqli->set_charset("utf8")) {
		printf("Error loading character set utf8: %s\n", $mysqli->error);

	} else {
		printf("Current character set: %s\n", $mysqli->character_set_name());
  	}
    
	$mysqli->close();

By following above steps, you can easily solve language issues with the help of MySQL and PHP. With the implementation of above steps you can resolve the multi-languages issues easily in almost most of the cases. If the issue still persists, you can contact one of our technical experts at info@cmsminds.com. cmsMinds has implemented many web projects where multi-language skill is required. Some of the other projects involved multi-currencies and payment integration to accept different currencies. We’ve implemented solutions in WordPress, Drupal and PHP, and can help you on the same.

Quick Inquiry


captcha