I'm having an issue with exporting data from a utf8 mysql database into an excel sheet with PHPExcel and retaining chinese characters. My mysql db is utf8 and has numerous chinese characters in it, and I'm exporting that data into a multi-sheet xls file (excel5) and every export the chinese characters turn into "?". I've tried utf8_encode() but it doesn't work for me. I also tried changing the output to excel2007 thinking it would be an excel5 writer issue.
Is there a way to get Chinese characters to export correctly?
Do I have to make the entire php file utf8? and if so how would I go about doing so?
Here is the portion I'm having issue with:
$res2 = mysql_query("SHOW COLUMNS FROM ".$sheetnametemp);
while($row2 = mysql_fetch_array($res2, MYSQL_NUM)) {
$counter = 2;
$cell = $coltemp;
$cell .= $counter;
$objPHPExcel->getActiveSheet()->SetCellValue($cell, $row2[0]);
$result = mysql_query("SELECT * FROM ".$sheetnametemp);
while($row = mysql_fetch_array($result))
{
$counter++;
$cell2 = $coltemp;
$cell2 .= $counter;
utf8_encode($row[$row2[0]]);
echo "<br />";
$objPHPExcel->getActiveSheet()->SetCellValue($cell2,utf8_encode($row[$row2[0]]));
}
I need to use those chinese characters as it is a multi-lingual catalog db so changing it to english wouldn't help. Also I am currently on a Mac with Xampp if that info is helpful in anyway.
Setting your executing "SET NAMES utf8" or mysql_set_charset("UTF8", $conn); to ensure that your connection to the database is using UTF-8.
You can test the connection using echo mysql_client_encoding($conn);
Make sure that you're not trying to call utf8_encode() anymore either.
Note: An Excel file uses a codepage to identify the character set being used within the file, but PHPExcel sets that codepage to a hard-coded value of UTF-8 internally, so all string content in the file should be UTF-8, and Excel knows to interpret the worksheets in the MS Excel GUI as UTF-8 as well. If you're not seeing chinese characters when you look at the spreadsheet in Excel itself, then check the language settings in MS Excel to see if it's configured to handle chinese
Credit: #Mark Baker.
Thanks again.
Related
I'm trying to do a select from a DB2 through PHP and odbc and then save those values on a file. The OS where the code is being executed is Debian. What I do is the following:
$query = "SELECT NAME FROM DATABASE_EXAMPLE.TABLE_EXAMPLE";
$result = odbc_prepare($server, $query);
$success = odbc_execute($result);
$linias = "";
if ($success) {
while ($myRow = odbc_fetch_array($result)) {
$linias .=format_word($myRow['NAME'], 30) . "\r\n";
}
generate_file($linias);
function format_word($paraula, $longitut) {
return str_pad(utf8_encode($paraula), $longitut, " ", STR_PAD_LEFT);
}
function generate_file($linias) {
$nom_fitxer = date('YmdGis');
file_put_contents($nom_fitxer . ".tmp", $linias);
rename($nom_fitxer . '.tmp', $nom_fitxer . '.itf');
}
The problem is that some of the retrieved values contains spanish letters and accents. To make and example, one of the values is "ÁNGULO". If I var_dump the code on my browser I get the word fine, but when it's write into the file it apends weird characters on it (that's why I think there is a problem with the charset). I have tried different workarounds but it just make it worst. The file opened with Notepad++ (with UTF8 encoding enabled) looks like:
Is there a function in PHP that translate between charsets?
Edit
Following erg instructions I do further research:
The DB2 database use IBM284 charset, as I found executing the next command:
select table_schema, table_name, column_name, character_set_name from SYSIBM.COLUMNS
Firefox says the page is encoded as Unicode.
If i do:
var_dump(mb_detect_encoding($paraula));
I get bool(false) as a result.
I have changed my function for formating the word hoping that iconv resolve the conflict:
function format_word($paraula, $longitut) {
$paraula : mb_convert_encoding($paraula, 'UTF-8');
$paraula= iconv("IBM284", "UTF-8", $paraula);
return $paraula;
}
But it doesn't. Seems like the ODBC it's doing some codification bad and that is what mess the data. How can I modify the odbc to codificate to the right charset? I have seen some on Linux changing the locale, but if I execute the command locale on the PC I get:
LC_NAME="es_ES.UTF-8"
LC_ADDRESS="es_ES.UTF-8"
...
I will try to summarize from the comments into an answer:
First note that PHPs utf8_encode will convert from ISO-8859-1 to utf-8. If your database / ODBC-Driver does not return ISO-8859-1 encoded strings, PHPs utf8_encode will fail or return garbage.
The easiest solution should be to let the database / driver convert the values to correct encoding, using its CAST function: https://www.ibm.com/support/knowledgecenter/SSEPEK_11.0.0/sqlref/src/tpc/db2z_castspecification.html
Try to alter your query to let DB2 convert everything to UTF-8 directly and omit the utf8_encode call. This can be done by altering you query to something like:
SELECT CAST(NAME AS VARCHAR(255) CCSID 1208) FROM DATABASE_EXAMPLE.TABLE_EXAMPLE
Thanks to Sergei for the note about CCSID 1208 on IBM PUA. I changed CCSID UNICODE to CCSID 1208.
I do not have DB2 at hand here, so the above query is untested. I'm not sure if this will return utf-8 or utf-16..
I am doing migration to generate SQL from one DB to another.
I am trying to get the output
But when I did a mb_convert_encoding("Mr.Wang (王老板)", 'UTF-8', 'Windows-1252')
I have the output as
I have those two extra "box". Any idea what am I doing wrong?
phpMyAdmin is able to export my old database containing chinese text in correct format, how do it do that in script?
*updated the images to better show my view
Have you tried setting the header in the script to UTF8? What I normally use is the following:
header('Content-Type: text/html; charset=utf-8');
That has worked for me so far for German characters & some Arabic & Japanese etc.
I found that I actually need to
mysql_query("SET NAMES 'utf8'");
before my select statement. And I do not need to run mb_convert_encoding("Mr.Wang (王老板)", 'UTF-8', 'Windows-1252') at all.
Now if I write my insert sql I got the correct text i wanted.
I created a simple code for uploading pictures to a folder, with PHP.
On the server side I have
<?php
header('Content-Type: text/plain; charset=utf-8');
//check if file is actually an image etc.
//if is an image, send it to "upload" folder
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
//save to the database a string like "upload/myImage.jpg", so I can render it on the site later
$stu = $dbh->prepare("UPDATE multi SET m_place=:name WHERE m_id = :id");
$stu->bindParam(':name', $n, PDO::PARAM_STR);
$n= "upload/".$_FILES["file"]["name"];
$stu->execute();
The problem?
If the name of the image is in english in the folder I see a "myImage01.jpg" and in the database also "upload/myImage01.jpg". But, if the name of the image is in greek in the folder I see "χωΟΞ―Ο‚ τίτλο.jpg" and in the db "upload/χωΟΞ―Ο‚ τίτλο.jpg".Which is wrong. Insted of χωΟΞ―Ο‚ τίτλο I should get "χωρις τιτλο" (thats greek for "no title" btw). So , I guess charset problem?
How do I fix this?
Thanks in advance
It sounds like your database doesn't have the correct collation. Make sure the tables/columns are using utf8_general_ci for their collation.
Also extremely important when handling UTF8 is to use the following two MySQL lines for GET requests...
SET time_zone = '+00:00'
SET CHARACTER SET 'utf8'
...and when you have a POST request use the following two...
SET time_zone = '+00:00'
SET NAMES 'utf8'
These will help ensure that UTF8 characters are maintained correctly.
Finally, I figure out that "PHP filesystem functions can only handle characters that are in system codepage". Thanks to this I solve my problem.
I used the iconv function
So I changed the move_uploaded_file line like so
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . iconv('UTF-8', 'Windows1253',$_FILES["file"]["name"]));
I am trying to export data from mysql database to a CSV file, but Thai content is being displayed as ????s. Also, in database it's not taking whatever I enter. If I enter ปดิวรัดา , it's saving as ปดิวรัดา
Please help me.
EDIT: Now, i edited encoding of table and fields to utf8_general_ci and its storing data as i entered into database but still export problem exists.
Original code we have written is here
Set your database encoding to utf8_general_ci
Also check that your column with strings has encoding utf8_general_ci.
Add this to the beginning if the $buffer you're exporting:
$buffer = chr(239) . chr(187) . chr(191) . $buffer;
The encoding is UTF with BOM, adding this will add the BOM.
I just learned about character sets today, so forgive the newb factor if this is confusing. Please ask for clarification if it's needed.
I wrote a program in php which recursively goes through the files in a folder and stores the file names in a database. The file names are then all exported from the database in json format using the json_encode($array) function.
However this function only works with UTF-8 encoded data. And since a few of the key-value pairs in the json export have the value of null, I'm lead to believe that those strings of filenames taken from the database are in fact not utf-8.
I've ensured that all the data going in and out of the the database is utf-8 by setting the defaults to utf-8 in my.cnf and restarting mysql from the command line using service mysql restart
[client]
default-character-set=utf8
[mysqld]
default-character-set = utf8
I then created my database, the table and all the columns in the table and confirmed that the database, table and all the columns are in fact utf-8
Checks if database is utf-8
SELECT default_character_set_name FROM information_schema.SCHEMATA S
WHERE schema_name = "schemaname";
Checks if table is utf-8
SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "schemaname"
AND T.table_name = "tablename";
Checks if field is utf-8
SELECT character_set_name FROM information_schema.`COLUMNS` C
WHERE table_schema = "schemaname"
AND table_name = "tablename"
AND column_name = "columnname";
There's this file that has the characters –µ–ª–∫—É–Ω—á–∏–∫ in the file name. When it's stored in the database the values appear as –©–µ–ª–â'.
Per my database settings, are all the strings going in and out of my database utf-8?
What can I do to ensure the data I am SELECT'ing from the database is utf-8, so I can perform json_encode($array)? (NOTE: this function only works on utf-8 encoded data)
Unfortunately I don't know how you can ensure everything coming out is UTF-8 (now I'm curious too!), but a starting point would be trying this in your PHP:
$encodedNames = array();
$errors = array();
// Loop through all of the filenames
foreach($filenames as $filename)
{
// Check if it's UTF-8 encoded
if('UTF-8' === mb_detect_encoding($filename, 'UTF-8', true))
{
$encodedNames[] = $filename;
}
else
{
$errors[] = $filename;
}
}
// json_encode the UTF-8 filenames
$jsonString = json_encode($encodedNames);
// Log the other filenames here so you can deal with them later...
http://php.net/manual/en/function.mb-detect-encoding.php