I have a table where the data are stored like boîtes with none utf8 characters. Now I have my php script which works fine on my local machine.
$utf = utf8_decode($details);
echo "UTF8-DE : ".$utf."<hr>";-> `boîtes`
When I put this script on another machine its not working its echoing boîtes . I am sure it depends on the charset of the php or server? Any help please
Try set_charset function
For mySqli
mysqli_set_charset($connection, "utf8");
For mySql
mysql_set_charset("UTF8", $connection);
Alternative
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
Set it up with headers
header('Content-type: text/html; charset=utf-8');
Related
I have a table where the data are stored like boîtes with none utf8 characters. Now I have my php script which works fine on my local machine.
$utf = utf8_decode($details);
echo "UTF8-DE : ".$utf."<hr>";-> `boîtes`
When I put this script on another machine its not working its echoing boîtes . I am sure it depends on the charset of the php or server? Any help please
Try set_charset function
For mySqli
mysqli_set_charset($connection, "utf8");
For mySql
mysql_set_charset("UTF8", $connection);
Alternative
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
Set it up with headers
header('Content-type: text/html; charset=utf-8');
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've made a simple PHP page to get the POST data and fetch a sql query then print the result. I'm using the mysql_fetch_array function.
The code works great but the response is a non-Unicode text, and it returns something like this:
?????ABC?????
note that the database collation is UTF8 and data stored are shown correctly in phpMyAdmin. I even used this META tag in php page but it results the same:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
Any idea?!
Add these lines of code before the first query:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET SESSION collation_connection = 'utf8_unicode_ci'");
Or you can edit your mysql configuration to use utf8 by default. Check here for what you need to do.
Change MySQL default character set to UTF-8 in my.cnf?
UPDATE
The function mysql_query is deprecated, so mysqli object can be used like so:
$mysqli = new mysqli("localhost", "MYSQL_USER", "MYSQL_PASS", "MYSQL_DB");
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");
$mysqli->query("SET SESSION collation_connection = 'utf8_unicode_ci'");
mysql_set_charset('utf8', $link);
Where $link is a connection created with mysql_connect
Try using mb_internal_encoding("UTF-8"); for details http://php.net/manual/en/function.mb-internal-encoding.php
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!