PHP + MySQL encoding issues for Portuguese (PT-Br) - php

I'm developing a website in Brazilian Portuguese and I'm facing some really annoying encoding problems.
Words that should be written this way: óbito are being written this way: �bito
I have noticed that while the texts are still at the database they are ok. But when I use echo with PHP, the encoding problem comes up.
List of things I have already done and did not help:
1- Set the PHP files to be saved as UTF-8
2- I'm using this meta tag <meta http-equiv="content-type" content="text/html; charset=utf-8" />
3- I used this SQL Queries:
CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
ALTER DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
ALTER TABLE tbl_name
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
;

Dont try to reinvent the wheel, keep things simple:
just use the following line after selecting the database:
mysql_query("SET NAMES 'utf8'") OR die(mysql_error());

You can change the charset using this function:
$q = mysql_set_charset('utf8');
var_dump($q);
(if the function returns true it's been successful). This should fix your problems for the connection.
For older versions of PHP, you can use the following:
<?php
if (function_exists('mysql_set_charset') === false) {
/**
* Sets the client character set.
*
* Note: This function requires MySQL 5.0.7 or later.
*
* #see http://www.php.net/mysql-set-charset
* #param string $charset A valid character set name
* #param resource $link_identifier The MySQL connection
* #return TRUE on success or FALSE on failure
*/
function mysql_set_charset($charset, $link_identifier = null)
{
if ($link_identifier == null) {
return mysql_query('SET NAMES "'.$charset.'"');
} else {
return mysql_query('SET NAMES "'.$charset.'"', $link_identifier);
}
}
}
?>
It seems like PHP uses latin1 by default and I can't find a way to change the default. So I guess you'll have to use mysql_set_charset() every time you start a new connection.
Boa Sorte.

Mine is solved by putting:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
in the header of the page, besides creating the tables again and changing the collation of database to utf-8 encoding.

Use this for Portuguese :
<meta http-equiv="Content-Type" content="text/html; charset=pt-BR" />

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Putting above meta tag inside <head> tag of your html/php document.
And you also need to run this query mysql_query("SET NAMES 'utf8'"); in your main functions file if you are using MySQL database with PHP.

Is solved by Putting: After the database connection string
$con = mysqli_connect('localhost', 'root', 'password', 'testdb');
if (mysqli_connect_errno()) {
exit("Failed to connect to MySQL: " . mysqli_connect_error());
}
mysqli_set_charset($con, 'utf8');

Related

Insert an Arabic text MySQL

I'm trying to store an Arabic text to the table, I searched a lot but I didn't find a solution that worked for me, so this is what I got:
$en = "OK";
$ar = "حسناً";
$link->query("INSERT INTO words (en,ar) VALUES ($en,$ar)");
The problem is when I insert it, the Arabic text looks like حسناً, my table's collation and MySQL's are utf8_general_ci, so is my database's, I also have mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');, but it doesn't work.
I recently had the same issues myself.
Here's a few pointers:
ALL attributes must be set to ut8 (collation is NOT the same as charset)
Save the document as UTF-8 (If you're using Notepad++, it's Format -> Convert to UFT-8)
The header in both PHP and HTML should be set to UTF-8 (HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and PHP: header('Content-Type: text/html; charset=utf-8');
Upon connecting to the databse, set the charset ti UTF-8 there as well, like this: $link->set_charset("utf8"); (directly after connecting)
Also make sure your database and tables are set to UTF-8, you can do that like this:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Remember that EVERYTHING needs to be set to UFT-8 charcode, or else it'll insert stuff like "حسناً". Hope this helped!
First thing is the database, table, and column. See if utf8 is set:
utf8_general_ci or utf8_unicode_ci
Also, the connection collation: utf8_general_ci
In PHP, after I connect with mysqli, I issue this:
$conn->query("SET NAMES 'utf8'");
And the web page output always jams in:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Here is a solution that worked for me:
when you create the database choose the collation for: utf8_unicode_ci
when you connect to the database from php use the following character set:
$dsn = "mysql:host=localhost;dbname=record;charset=utf8";

How to insert an hebrew value into a mysql db in php

I'm trying to insert an hebrew value into my mysql db, instead of hebrew the values looks like that.
שדגשדכעשד
The collation of the table is latin1_swedish_ci by default, I tried also to change to utf-8_general_ci, hebrew_bin, hebrew_general_ci but the result is still the same.
In my code I'm using of course the meta tag to configure the charset:
<meta charset="UTF-8">
And before my php query I added this line:
mysql_query("SET NAMES utf8");
I'm viewing the result in the phpmyadmin.
I have solved my Hebrew language problem. It was a database and table row/field encoding issue. Here is the solution I used. I took help from another answer and the link is given below, in case anyone needs it.
The database collation has to be utf8_general_ci.
The collation of the table with Hebrew has to be utf8_general_ci.
In the PHP connection script put
header('Content-Type: text/html; charset=utf-8');
In the xhtml head tag put
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
If you are using MySQLi put this code in the connection script after selecting the database:
mysql_query("SET NAMES 'utf8'");
If you are using PDO, put
$conn->query("SET NAMES 'utf8'");
The first answer helped me and I took it from there
Set charset to achieve the solution
While creating the database
CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
Or if the database is already created
CREATE TABLE table_name(
...
)
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
OR while writing query
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
$re = mysql_query('SHOW VARIABLES LIKE "%character_set%";')or die(mysql_error());
while ($r = mysql_fetch_assoc($re)) {var_dump ($r); echo "<br />";}
Check the collation_connection:
show variables like '%collation%'
you should make sure that:
you set utf-8 in php
you use utf-8 in the connection
your table is defined as utf-8_general_ci
the specific field is defined as utf-8_general_ci
Then you should be able to view Hebrew, or any other language, correctly in phpadmin
what finally helped me is to add the charset to the connection:
{"mysql:host=$host;dbname=$db;charset=utf8"}
I would say, in order to make sure that the values are passed well to the database I would add a die statment before inserting to the database and print the value, example:
die($_POST['thevalue']);
//insert to database.
//...
If it goes well, then the problem is on the database side, on the database I would try with this collation
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
as per http://dev.mysql.com/doc/refman/5.0/en/charset-mysql.html suggest.
But if it fail on the php side, reason can be, the server does not support Hebrew, make sure that on the html output document you use the correct metatag with
...
<meta charset="ISO 8859-8">
...
Let us knows how it proceed, good luck :)
For future use, if you have this issue and you are using PDO and not mysqli, you will need to do it like this:
The Database (all of it) collation has to be utf8_general_ci.
Set the collation of the table with Hebrew also to utf8_general_ci.
In the HTML "head" tag, add this: < meta charset="utf-8" >
In your PHP connection file, add after the connection Query this line: conn->exec("set names utf8");
if you are using classes, then you will probably have the "conn" as a variable.
in that case, you will have to use it like this:
$this->conn->exec("set names utf8");
Hope this helps to future people that have this problem and using PDO.
Best of luck.
I think I figure it out:
here's my code:
$conn = mysqli_connect($dbserver,$dbuser,$dbpwd,$dbname);
if (mysqli_connect_errno()){
printf("Connection failed: %s\n" , mysqli_connect_errno());
exit();
}
printf("Initial character set: %s\n", mysqli_character_set_name($conn));
/* change character set to utf8 */
if (!mysqli_set_charset($conn, "hebrew")) {
printf("Error loading character set hebrew: %s\n", mysqli_error($conn));
exit();
} else {
printf("Current character set: %s\n", mysqli_character_set_name($conn));
}

MySQL, HTML, and PHP character conflicts

I'm trying to pull text from a database. In the database, it displays as "♦" with no problem.
However, when I pull the record from the database, it displays on the page as a question mark (not the black box with a white ? in the middle), a plain old "end of question" question mark.
For instance:
♦ We Need To Pay You ♦
Will display:
? We Need to Pay You ?
I have the database structure setup as utf8-bin because latin didn't save them right. I've done hours of looking around, reading articles, and cannot find an answer anywhere. There has to be a way to get it to pull from the database the right way. I can insert it but can't retrieve it which makes no sense.
All help is appreciated.
Save your php file as Encode UTF-8 without BOM
set before your query: mysql_set_charset('utf8',$connect) or
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") if you are using PDO
set inside your <head> tag: <meta http-equiv="content-type"
content="text/html; charset=utf-8">
Try this in your php file:
echo utf8_encode(text) or utf8_decode(text)
And this in your html head:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
set the charset with mysql_set_charset.
bool mysql_set_charset ( string $charset [, resource $link_identifier = NULL ] )
and be sure your page content charset is utf-8
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
You can use the answer I gave here to check for the source of the problem: How to fix "Incorrect string value" errors?
In your case most likely the HTML is not UTF-8 encoded. Depending on the HTML version you are using, one of those will fit your needs: http://en.wikipedia.org/wiki/Character_encodings_in_HTML
One thing left: Check, that your IDE/Editor also is set to store the files in UTF-8
Well, you need to decide what encode you will use on your application, IMHO utf8 is the best encoding, so you need to make you IDE ou Text Editor works with UTF-8 only and tell browser that your page is UTF-8 using:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
And using a modern connection way with PDO:
<?php
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=test',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
Creating a database using UTF-8:
CREATE DATABASE "test" CHARACTER SET utf8 COLLATE utf8_general_ci
Creating a table using UTF-8:
CREATE TABLE `test` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 40 ) NOT NULL
) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
Using utf8_encode and utf8_decode is pointless, it will make your code unreadable.
Sorry my english, i'm brazilian :D

How to store the data in unicode in hindi language

I am using PHP and MySQL for an application.
Questions are:
How to store data in MySQL नवीन खेतिहर उपकरण। readable format or निलेस र पà¥à¤¬ format
When user enters data in a textbox and clicks on submit, at that time we get data in different format. What we need to do should we convert and store in MySQL in readable format.
Choose utf8 character set and utf8_general_ci collation.
Obviously, Collation of the field (to which you want to store Hindi text) should be utf8_general_ci.
To alter your table field, run
ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>` VARCHAR(100)
CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;
Once you've connected to database, run the following statement at first
mysql_set_charset('utf8');
Eg:
//setting character set
mysql_set_charset('utf8');
//insert Hindi text
mysql_query("INSERT INTO ....");
To retrieve data
//setting character set
mysql_set_charset('utf8');
//select Hindi text
mysql_query("SELECT * FROM ....");
Before you printing any unicode text (say Hindi text) on browser, you should have to set content type of that page by adding a meta tag
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Eg:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Unicode</title>
</head>
<body>
<?php echo $hindiText; ?>
</body>
</html>
Update:
mysql_query("SET CHARACTER SET utf8") has changed tomysql_set_charset('utf8');
This is the preferred way to change the charset. Using mysql_query() to set it (such as SET NAMES utf8) is not recommended. See http://php.net/manual/en/function.mysql-set-charset.php*
You only need to run the following command on the table.
ALTER TABLE <tablename> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Note: You should use utf8_general_ci instead of ut8_unicode_ci, as it is said to be faster. What's the difference between utf8_general_ci and utf8_unicode_ci
There is no need to alter table with charset. but your database should be with utf8_unicode_ci.
while connecting with database just need to add this line only." mysqli_set_charset($con, 'utf8')"
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
mysqli_set_charset($con, 'utf8');
mysqli_query($con, $sql);
After converting COLLATE to utf8_general_ci use this line
mysqli_set_charset($con, 'utf8'); in between $con and mysqli_query() below your query.
$con= mysqli_connect($host,$user,$pass,$db) or die('Unable to connect');
$sql="your query.. ";
mysqli_set_charset($con, 'utf8');
$result=mysqli_query($con,$sql);

Having trouble with Php, Mysql and UTF8

Problem, simple and annoying.
Im just trying to print a list of names, collected from my mysql database.
The PHP-files are saved in utf8, the database and tables are set to use utf8. Still 'å,ä,ö', for example, outputs as �. Can't believe I'm still having this issue.
Of course, Latin1 solves the problem. The thing is that I have to use utf8 since I'm doing some json_encode for sending the data to an ajax-script.
Any idea what on earth could be wrong?
Should I convert the data to utf8 before returning it perhaps? Seems weird I should have to..
Convert utf8_general_ci to utf8_unicode_ci...
Try running SET NAMES UTF8 query after you connect to database...
function connect($server, $database, $username, $password, $charset = "UTF8"){
$link = mysql_connect($server, $database, $password);
if(!$link){
die("Unable to connect to database server.");
}
mysql_selectdb($database);
if(function_exists("mysql_set_charset")){
mysql_set_charset($charset, $link);
}else{
mysql_query("SET NAMES $charset");
}
}
Also make sure you have this (or via header()) in your HTML...
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Two things to do...
Make sure your HTML header is sending the correct charset:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Use utf_encode() when adding your names to the array. The line in your should be
$guests[] = array_map('utf8_encode', $row);

Categories