Problems with ÆØÅ (Norwegian letters) when inserting into database (MySQLi) - php

I'm having problems when inserting Norwegian letters (æøå) into a database. The charset coding of my document (using Notepad++) is set to UTF-8. The variable that is being inserted has the proper characters, but when it is inserted a word that should be shown as "spørsmål", is when inserted to the database, shown as "spørsmÃ¥l".
I'm using the following code to insert:
$newContent = htmlspecialchars($_POST['newContent']);
$stmt = $mysqli->prepare("UPDATE info SET frontpageText=?");
$stmt->bind_param('s', $newContent);
$stmt->execute();
$stmt->close();
And when connecting to the databse, I've tried using
$mysqli->set_charset("utf8");
$mysqli->query("SET NAMES 'utf8'");
I've also done the following:
The database collation is set to utf8_danish_ci
Using header('Content-type: text/html; charset=utf-8'); (PHP header)
Using meta charset="utf-8" (in the HTML-head)
The document itself is encoded in utf-8
Tried running SET NAMES utf8;
The worst part about this, is that I've actually had it working earlier, but I've appearantly broken something (which I don't know what).
Anyone has an idea what could be done to fix this issue?
EDIT: Problem has been solved. Apparently the table wasn't properly set to UTF-8. Ran this code in phpMyAdmin
ALTER TABLE table_name CHARSET = 'utf8';

The character set and the collation have separate encodings (one can be latin1 and the other can be utf8).
A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set. Let's make the distinction clear with an example of an imaginary character set.
To diagnose the encoding of a table this query can be run, where ##TABLENAME## should be the actual table name.
SHOW CREATE TABLE ##TABLENAME##
If the encoding is not utf8 it can be altered with
alter table table_name charset = 'utf8';
Here's a thread on collation vs. character set, What does character set and collation mean exactly?.

You can convert all characters to html chars and save into database that way.
mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8');
All browsers will display it in the right way.

Make sure the default table charset or field charset is set to utf-8.
I know you've said that database charset is set correctly, but tables may interfere with that if they specify a different charset.

Related

MySQL won't store "€"

I am working on a application where I need to store/show data containing many special characters. I have set database collation utf8. I have set collation of table utf8 and character set as utf8_unicode_ci. It is storing all special characters like é, â. But whenever a character ,€ comes it isn't stored as it is. Like whenever there is a word “attributed†it becomes âattributedâ. I am currently using Laravel 5.2 (PHP) .
What I have tried so far
I have set following in my code
iconv_set_encoding('internal_encoding', 'UTF-8');
mb_internal_encoding('UTF-8');
I have also tried
$value = array_map("utf8_encode", $array);
But this special character isn't getting stored as it is. Will any one let me know what should I do to get this special character saved as it is.
try setting your collation to "utf8_general_ci" in your mysql
Normally its no problem to store the € sign to a database field. Check if all your scripts are in a correct coding.
Set your table and all data in that table to utf8_general_ci then try to change your php file to UTF-8.
header('Content-Type: text/html; charset=utf-8');
And if you have incorrect data then use.
utf8_encode("test");
to encode your string to a correct UTF-8 string. If that isn't working i think your data or string your try to convert is not correct.
For being able to use UTF8 characters in your queries you must run a certain query setting this, just like this:
$db->query("SET NAMES UTF8");
But I've seen that you said you're using Laravel. I haven't been working on it, but I guess this's automatically set by the charset parameter written in config, just like in the screenshot of this question:
Laravel UTF-8 To Database

Display special characters from table in php page

I have a table which contains name of teachers. Some names are like René Visser having special characters. When I write the SQL query for displaying the name, the special characters are replaced by � symbols.
I have tried cast() but it's not working properly. My query is like this.
$qry = mssql_query("SELECT CAST(FirstName_1 AS NVARCHAR(250)) AS Name FROM
tbl_teachers");
The FirstName_1 column is nvarchar type. I have tried to cast FirstName_1 to VARBINARY(8000), then casting result to IMAGE like following.
CAST(CAST(FirstName_1 AS VARBINARY(8000)) AS IMAGE) AS Name.
You should have UTF-8 encoding for the SQL server.
Then, make sure you send the encoding headers also from php using :
header('Content-Type: text/html; charset=utf-8');
You will need to specify the charset, or if you have already, set it to Windows-1252. It's likely your page is reading in the data with UTF-8 encoding. Which explains the ? symbols.
<head>
<meta charset="Windows-1252">
</head>
You most likely have a charset issue. Your query has little to do with this, you don't need to cast it.
You'll need to set the charset of the connection, the PHP and HTML header and the document itself as the same charset. UTF-8 will most likely cover all of the special characters you'll ever need.
Below is some things you could do.
ini_set('mssql.charset', 'UTF-8'); (Have this run upon connecting to your database)
Set both PHP and HTML header to UTF-8
PHP: header('Content-Type: text/html; charset=utf-8'); (has to be called before any and all output)
HTML: <meta charset="utf-8"> (has to be inside the <head>-tag
Save the document in UTF-8 encoding. If you're doing it in Notepad++, it's Format -> Convert to UFT-8 (you may also choose UTF-8 w/o BOM)
The database itself, and it's tables, may need to be set to UTF-8. This can be done with the query below (need only to be run once):
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Keep in mind that all parts of your application has to be set to the same type of charset, or you'll experience those kind of things in your database.

Reading Unicode characters from MySQL with PHP

I've inherited a MySQL database which contains a field named Description of type text and collation of latin1_swedish_ci.
The problem with this field is it contains utf-8 data with some Unicode characters, e.g. character 733, etc. Sometimes this character also exists in the field represented as HTML encoded "&#733" as well.
I'm trying to read the table and export the data to a CSV file and I need to represent this character as a double quote.
Reading the HTML encoded character is easy enough. However, it appears that the actual Unicode character is converted to utf-8 before I can do anything with it resulting in a "?".
How do I read in the Unicode character 733 (U+02DD), recognize it and convert it?
Here's a simplified (not tested) version of the code.
<?
$testconn=odbc_connect ("TESTLIB", "......", "......");
$query="SELECT Description FROM TestTable";
$rsWeb=mysql_query($query));
$WebRow=mysql_fetch_row($rsWeb));
$Desc = $WebRow[0];
$Desc = str_replace('"','""',$Desc);
fwrite($output,"\"".$Desc."\",\r\n");
%>
Also set charset to utf-8 when connecting to SQL server:
http://php.net/manual/en/mysqli.set-charset.php
$mysqli->set_charset("utf8");
I think your connection charset is not utf8, that's why chars are being converted to '?'.
Read this: http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
Post result for query:
show variables like 'char%';
You really should put only non-entity (Unicode) version in the database, and entity-decode the rest. However, when you want to use UTF-8 with MySQL, there are a few things to remember:
Your table column's collation should be utf8_bin or similar.
Your table's collation and database collation should also be utf8_bin just in case.
Your connection charset should be UTF8. Do this by executing the "SET NAMES utf8" query.
Also, if you're outputting a HTML page, that should have the UTF8 charset as well. If everything is correct, the UTF8 characters should come out fine.
Good luck!

how to display this chinese character?

I already set mySQL Collation as utf8_unicode_ci, if I manual insert chinese character in my database, it's successful work to displaying chinese character, but once i use my code, it was display this
ã€å¼µç‘žæŒ¯ã€æ±Ÿç¥¥ç¶�..
I had add this <meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta> in my header, I had try utf8_encode ,but still the same problem happen.
thank you and hope you guy reply me soonest
Have you set the connection character set / collation? Execute this query immediately after creating a connection
SET NAMES 'utf8'
You have probably forgot to execute this query after connecting
SET NAMES utf8
Try it and you'll see
there 2 steps you should do
add a query with UTF8
$query = "SET NAMES 'utf8'";
mysql_query($query);
make sure your file is encoded as UTF8,
open your script with your favourite editor and save as UTF8
To ensure MySQL expects UTF-8 encoding by default from client connections, use the following query:
SET NAMES 'utf8'
In addition, make sure PHP interprets the string as UTF-8 string. Since PHP does not support multibyte characters, you must use a function to allow PHP to work with UTF-8 strings:
utf8_decode()
or something.

€ char is shown as ? in UTF8 Output

I have reworked a website and now it is xhtml valid etc and using UTF8. Everything is fine, but if anywhere in the Database is a Euro-char it is just displayed as a questionmark.
What would be the right way to fix this?
As output is done by Typo3 i cant change much about that.
Try executing these queries before the queries that fetch data:
SET NAMES utf8
SET CHARACTER SET utf8
This might be due to wrong database connection encoding
Lookup SET NAMES sql statement
$db_link = mysql_connect($host,$user,$passwd);
mysql_query("SET CHARACTER SET 'utf8';", $db_link);
mysql_query("SET NAMES 'utf8';", $db_link);
DON'T issue both statements!
Don't issue
SET NAMES utf8
SET CHARACTER SET utf8
one after another. It can cause trouble. I already had bad experience with SET CHARACTER SET utf8 right after SET NAMES utf8.
I recommend to issue only SET NAMES ...
MySQL docs has explanations why: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
In short: SET NAMES ... sets connection's charset to the same as client ans result charset. while SET CHARACTER NAME... sets different connection's charset and collation.
Please read the doc and decide whichever it better in your case. Or even better make a test.
What charset (encoding, collation,...) are you using for the database column that contains the € sign?
The problem could be that your data stored in this column ist mixed up because the € sign is a somehow difficult beast when not using UTF-8 character encoding. The problem ist that the € sign is encoded as \xA4 when using ISO-8859-15 and as \x80 when using Windows-1252 (the common Western-European charset on Windows machines).
If your data inside the column is not encoded correctly MySQL won't be able to transcode it into UTF-8 correctly - even if you use SET NAMES utf8.
If you're sure you have the right character data (0x20AC), it could also be the fonts ont he client-side. If the font you are using does not handle that particular character, you'll just see some default question mark.
However, why not use the escape code €, which gives you: €
Cheers,
You could try something like
$value = iconv('ISO-8859-1', 'UTF-8//TRANSLIT', $value);
The "ISO-8859-1" part may be different depending on your MySQL table character encoding.

Categories