At first we was using the default character set by the mysql that was latin1_swedish_ci. Then we had problem with other languages like russian etc. So we have now converted the tables into collation and the field into utf8_unicode_ci. We need some confirmation here should we stick with utf8_unicode_ci for the support all the possible languages or go for something else like utf8_bin?
Next issue for php files where we are using mysqli we have set this.
if (!mysqli_set_charset($link, "utf8")) {
echo("Error loading character set utf8: ". mysqli_error($link));
}
else {
echo("Current character set: ". mysqli_character_set_name($link));
}
Then for places where we use mysql only we did as below
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
The problem now we find this is like quite tedious and we are scared we might be miss. Is it possible to set this character setting in like a config file like we have one for our db connection?
Don't mix mysql_* with mysqli_* functions. You need to stay consistent! You use mysqli_ first and then you use mysql_. That won't work!
This is how I do it:
mysqli_set_charset($Handle, 'utf8'); // <- add this too
mysqli_query($Handle, "SET NAMES 'utf8';");
mysqli_query($Handle, "SET CHARACTER SET 'utf8';");
mysqli_query($Handle, "SET COLLATION_CONNECTION = 'utf8_unicode_ci';");
// might be a bit redundant but it's safe :) ... I think :)
Then make sure you provide proper UTF8 to it.
Related
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I can't understand whats problem.
I write data to mysql with php and utf-8 issues
Write in HTML form Binəqədi r. Çiçək qəs
Writed database Bin?q?di r. ÃÃÂ
When I use mysqli_set_charset($conn,"utf8"); in insert and update query
Writed database Bin?q?di r. Çiç?k q?s
Write data manually direct to database and use mysqli_set_charset($conn,"utf8"); in select works normal.
How can I fix it? INSERT and UPDATE with UTF-8.
This is my insert code
public function insert($sql) {
$conn = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"utf8");
// $conn->set_charset("utf8"); use same this.
if ($conn->query($sql) === TRUE) {
return 1;
} else {
return 0;
}
$conn->close();
}
NOTE: This issues on Linux and OSX. In windows work goods. How can I fix it for linux hosting?
try
AddDefaultCharset utf-8
in you apache configuration
or in you php file use
header( 'content-type: text/html; charset=utf-8' );
and also try to
default_charset = "utf-8";
in you php.ini
also you can try with
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-client-handshake = false #force encoding to uft8
character-set-server=utf8
collation-server=utf8_general_ci
[mysqld_safe]
default-character-set=utf8
in your my.ini and restart mysql demon
Please consider all of these:
Before your queries, do this:
$conn->query('SET NAMES utf8');
Be sure your file encoding is utf8 (not the html encoding). I mean the file encoding which your IDE is using.
Be sure about your table charset
This is clearly a duplicate of a well-known issue, but cannot be flagged for closure due to the bounty. So, the checklist:
Ensure database columns, tables, and database are set to character set utf8mb4
Ensure the connection is set to the same character set with $conn->set_charset("utf8mb4");
Ensure your server is configured to inform web browsers that it will be sending Unicode text. In PHP, do header("Content-Type: text/html;charset=utf8"); or set it up in your web server configuration.
Try using set names
public function insert($sql) {
$conn = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// $conn->set_charset("utf8");
$conn->query("SET NAMES 'utf8'");
if ($conn->query($sql) === TRUE) {
return 1;
} else {
return 0;
}
$conn->close();
}
From MySQL (http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html):
SET NAMES 'charset_name' [COLLATE 'collation_name']
SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)
A SET NAMES 'charset_name' statement is equivalent to these three statements:
SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET character_set_connection = charset_name;
Setting character_set_connection to charset_name also implicitly sets collation_connection to the default collation for charset_name. It is unnecessary to set that collation explicitly. To specify a particular collation, use the optional COLLATE clause:
SET CHARACTER SET charset_name
SET CHARACTER SET is similar to SET NAMES but sets character_set_connection and collation_connection to character_set_database and collation_database. A SET CHARACTER SET charset_name statement is equivalent to these three statements:
SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET collation_connection = ##collation_database;
Setting collation_connection also implicitly sets character_set_connection to the character set associated with the collation (equivalent to executing SET character_set_connection = ##character_set_database). It is unnecessary to set character_set_connection explicitly.
My opinion is that in some point the proper character set for the field description is not specified when using stored procedure. Do check again that your database/table/field have as character set utf8_general_ci
From MySQL (http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html):
Example: Suppose that column1 is defined as CHAR(5) CHARACTER SET latin2. If you do not say SET NAMES or SET CHARACTER SET, then for SELECT column1 FROM t, the server sends back all the values for column1 using the character set that the client specified when it connected. On the other hand, if you say SET NAMES 'latin1' or SET CHARACTER SET latin1 before issuing the SELECT statement, the server converts the latin2 values to latin1 just before sending results back. Conversion may be lossy if there are characters that are not in both character sets.
The gibberish you displayed seems to involve more than one error. Çiç, when Mojibaked to latin1 gives Çiç.
Please provide SELECT col, HEX(col) FROM ... for something that is 'bad'.
Meanwhile, your reply about the stored procedure -- Do SHOW CREATE PROCEDURE ... and check what character set was in effect when the procedure was created. That could be part of the problem. (I noticed that that decade-old bug report you mentioned failed to consider this aspect.)
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));
}
I need to insert korean text to my database which is coming by url get request. However, inserted values are not recognized in MySQL. Please, need quick instruction. Thanks.
setlocale(LC_CTYPE, 'ko_KR.utf8');
mb_internal_encoding("UTF-8");
mb_http_output('utf-8');
$p_text = rawurldecode($_GET["text"]);
Right after your mysql connection make this query
$mysqli->query("SET NAMES 'UTF8'");
or
mysql_query("SET NAMES 'UTF8'");
also make sure that your fields have utf8_general_ci collation
I have a small problem. I have russian characters in a database and I want to avoid setting character_set_results to utf8 all the time. Is there a way to hard set that ?
mysql_query("set character_set_results='utf8'");
I want to avoid this line and hard set it.
Thank you
mysql_query("SET NAMES 'utf8'");
For czech language I use:
mysql_query("set names latin2 collate latin2_czech_cs")
Don't forget to set the proper collation too!
When I insert some text written in Unicode into database, they become question marks. Database encoding is set to UTF-8. What else may be incorrect? When I check in phpMyAdmin there are question marks inserted only!
This is the code I use for connecting to database:
define ("DB_HOST", "localhost"); // Set database host
define ("DB_USER", "root"); // Set database user
define ("DB_PASS","password"); // Set database password
define ("DB_NAME","name"); // Set database name
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
mysql_set_charset('utf8',$link);
mysql_query("SET CHARACTER SET utf8");
Is the text you inserted encoded in UTF-8 too? Or is your PHP files not UTF-8? Have you set the MySQL Client connection to UTF-8?
If not, then that is probably the cause of the problem.
How do you know the become question marks? Do you see them as question marks on your PHP pages, when you output the database fields, or in software like phpMyAdmin?
Either way, the problem is probably the encoding of your web page rather than the database's. Make sure to add the following line:
header('Content-Type: text/html; charset=utf-8');
// First make sure your file produce UTF-8 characters
header('Content-Type: text/html; charset=utf-8');
// Make sure with your spelling
// write
mysql_query("SET CHARSET utf8");
// Instead of
mysql_query("SET CHARACTER SET utf8");
// For some reasons
mysql_query("SET CHARSET SET utf8");
// It works on some servers and for other servers not. I am not sure why.
// Try using mysql_set_charset("utf8"); only without mysql_query("SET CHARSET utf8");
// For me I had the same issue with my server
// When I used mysql_set_charset("utf8"); only --> the problem solved
// again make sure with your spelling and try again
Sorry, but you are all wrong...
My friend King Julien, you just have to execute:
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET CHARSET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'"); // This statement does the job!!! ;)
Have a nice day!