mysql php multilingual Arabic and hindi - php

I have a field "scroller" with collation "utf8_unicode_ci" in mysql database.
I am trying to save an arabic text to this field.
When i insert it to the mysql directly it works.
Using this code below i tried to edit the field. When i echo it it displays correctly. But when it is saved using update its saving like "??????? : 89 ????? ?? ?????? AMRI ????????? ?????? ???? ??????? ????? ????? ???????" in the database.
echo "<meta http-equiv=Content-Type content=text/html; charset=UTF-8>";
require 'config.php';
mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');
$scroller=htmlentities($_POST['scroller'],ENT_QUOTES,"UTF-8");
mb_internal_encoding("UTF-8");
echo $scroller;
$sql="UPDATE arab_scroller SET scroller='$scroller' WHERE page='$id1' ";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header( 'Location:index.php' );
Please help me out

I just tested same thing with Turkish. If your table's default collation is not UTF8, you might have these kind of problems.
Please try this first:
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
And then try to run your code again.
Also, it is better to use mysql_set_charset instead of SET NAMES:
http://php.net/manual/en/function.mysql-set-charset.php

You just change table Collation utf8_general_ci by default it takes latin;
OR
you run this query :
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
This is really works with me.

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 db question marks instead of hebrew characters..?

I'm trying to build a shopping cart using PHP & MySQL.
my db in MySQL is utf8 and my table in the db is utf8,
How can I use Hebrew characters?
I was able to solve this by doing the following:
the db collation has to be utf8_general_ci
the collation of the table with hebrew has to be utf8_general_ci
in your php connection script put header('Content-Type: text/html; charset=utf-8');
in xhtml head tag put <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
after selecting the db in the connection script put mysql_query("SET NAMES 'utf8'");
After a lot of work I found a solution that always works..:
without SET_NAMES.
In the conn.inc.php file, after you selected a database and connected to it, do this:
if(!mysqli_set_charset($conn, 'utf8')) {
echo 'the connection is not in utf8';
exit();
}
...and in the html always use charset utf-8;
That solved it for me. No need to use set_names(), which is ok but it annoyed the hell out of me.
You can use the PDO in your code like this:
$db = new PDO($config['DSN'], $config['dbUserName'], $config['dbPassword']);
$db->exec("SET NAMES 'utf8'");
Make sure you include the single quotation marks around the 'utf8'.
If it's an encoding problem (and it sounds like it is), this query will help:
SET NAMES utf8
Execute this query (e.g. mysql_query("SET NAMES utf8")) right after you connect and before you move any data.
More info
$conn->set_charset("utf8"); Use this for your dbconnect
Where are the question marks showing up? It may be an encoding problem somewhere other than in the data base.
To store non-ASCII characters in a database column, you need to define that column with a specific character set. You can specify a character set at 3 levels: database, table, and column. For example:
CREATE DATABASE db_name CHARACTER SET utf8
CREATE TABLE tbl_name (...) CHARACTER SET utf8
CREATE TABLE tbl_name (col_name CHAR(80) CHARACTER SET utf8, ...)
http://www.herongyang.com/PHP/Non-ASCII-MySQL-Store-Non-ASCII-Character-in-Database.html
In my case I created the DB from a dump, and this command solve it:
ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
mysql_query('SET NAMES utf8') ..... Put this in Php

Can't insert cyrillic text into mysql database

When I'm trying to insert cyrillic text into MySQL database it inserts it like:
г???????????? ?? ????????
Рісѓрїр°ріс‹рї р° с‹рір°рї
So, I have two pages: registration.php and addUser.php. In each of them
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Database consist of 11 tables, each table has collation: utf8_general_ci, type: MyISAM. Each field in every table has Collation: utf8_general_ci.
When I'm writing to database directly in phpMyAdmin and then show this data to web-page. In English and Russian - all OK.
But when I'm full my form with personal data on registration.php and then going to addUser.php - all cyrillic characters displayed like I wrote upper - on page and in database too.
function AddNewUser($Name, $Surname, $FatherName, $Email, $Password, $Phone, $DegreeID, $RankID,
$Organization, $Department, $Country, $City, $Address, $Job)
{
//fetch data from database for dropdown lists
//connect to db or die)
$db = mysql_connect($GLOBALS["gl_kussdbName"], $GLOBALS["gl_kussUserName"], $GLOBALS["gl_kussPassword"] ) or die ("Unable to connect");
//to prevenr ????? symbols in unicode - utf-8 coding
mysql_query("SET NAMES 'UTF8'");
//select database
mysql_select_db($GLOBALS["gl_kussDatabase"], $db);
$sql = "INSERT INTO UserDetails (
UserFirstName,
UserLastName,
UserFatherName,
UserEmail,
UserPassword,
UserPhone,
UserAcadDegreeID,
UserAcadRankID,
UserOrganization,
UserDepartment,
UserCountry,
UserCity,
UserAddress,
UserPosition)
VALUES(
'".$Name."',
'".$Surname."',
'".$FatherName."',
'".$Email."',
'".$Password."',
'".$Phone."',
'".$DegreeID."',
'".$RankID."',
'".$Organization."',
'".$Department."',
'".$Country."',
'".$City."',
'".$Address."',
'".$Job."'
);";
//execute SQL-query
$result = mysql_query($sql, $db);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
//close database = very inportant
mysql_close($db);
}
?>
There also such information in phpMyAdmin:
auto increment increment 1
auto increment offset 1
autocommit ON
automatic sp privileges ON
back log 50
basedir \usr\local\mysql-5.1\
big tables OFF
binlog cache size 32,768
binlog format STATEMENT
bulk insert buffer size 8,388,608
character set client utf8
(Global value) cp1251
character set connection utf8
(Global value) cp1251
character set database cp1251
character set filesystem binary
character set results utf8
(Global value) cp1251
character set server cp1251
character set system utf8
character sets dir \usr\local\mysql-5.1\share\charsets\
collation connection utf8_general_ci
(Global value) cp1251_general_ci
collation database cp1251_general_ci
collation server cp1251_general_ci
completion type 0
concurrent insert 1
So I need to properly show, save and select russian text from database. Thanx!
connect timeout 10
datadir \usr\local\mysql-5.1\data\
Try calling mysql_set_charset('utf8'); after connecting to the database. I think it's similar to executing a SET NAMES query, but since the PHP manual says using that function over a SET NAMES query is recommended, I'd try it.
Also, when you display your content, you could try echo htmlentities($string, ENT_COMPAT, 'UTF-8');
I store greek in tables created like ths:
CREATE TABLE `test` (
`test_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`test_name` VARCHAR(30) NOT NULL,
PRIMARY KEY (`test_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
Or if table already created I guess you can change the charset it in the phpmyadmin interface. Maybe this helps.
Check your MySQL configuration and ensure that your encoding is defined correctly.
Add these lines to my.cnf or my.ini, which ever your installation uses.
These settings did the trick for me:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
I have tried multiple collations in phpMyAdmin as well as changing the charset of the page, which didn;t make sense but i was willing to try anything after two days of research. this command helped me: mysql_set_charset('utf8');
Collation on the column was set to koi8r_general_ci
Edit your structure field to set collation utf16_general_ci.
After that insert your data.

Storing and displaying unicode string (हिन्दी) using PHP and MySQL

I have to store hindi text in a MySQL database, fetch it using a PHP script and display it on a webpage. I did the following:
I created a database and set its encoding to UTF-8 and also the collation to utf8_bin.
I added a varchar field in the table and set it to accept UTF-8 text in the charset property.
Then I set about adding data to it. Here I had to copy data from an existing site.
The hindi text looks like this: सूर्योदय:05:30
I directly copied this text into my database and used the PHP code echo(utf8_encode($string)) to display the data. Upon doing so the browser showed me "??????".
When I inserted the UTF equivalent of the text by going to "view source" in the browser, however, सूर्योदय translates into सूर्योदय.
If I enter and store सूर्योदय in the database, it converts perfectly.
So what I want to know is how I can directly store सूर्योदय into my database and fetch it and display it in my webpage using PHP.
Also, can anyone help me understand if there's a script which when I type in सूर्योदय, gives me सूर्योदय?
Solution Found
I wrote the following sample script which worked for me. Hope it helps someone else too
<html>
<head>
<title>Hindi</title></head>
<body>
<?php
include("connection.php"); //simple connection setting
$result = mysql_query("SET NAMES utf8"); //the main trick
$cmd = "select * from hindi";
$result = mysql_query($cmd);
while ($myrow = mysql_fetch_row($result))
{
echo ($myrow[0]);
}
?>
</body>
</html>
The dump for my database storing hindi utf strings is
CREATE TABLE `hindi` (
`data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `hindi` VALUES ('सूर्योदय');
Now my question is, how did it work without specifying "META" or header info?
Thanks!
Did you set proper charset in the HTML Head section?
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
or you can set content type in your php script using -
header( 'Content-Type: text/html; charset=utf-8' );
There are already some discussions here on StackOverflow - please have a look
How to make MySQL handle UTF-8 properly
setting utf8 with mysql through php
PHP/MySQL with encoding problems
So what i want to know is how can i
directly store सूर्योदय into my
database and fetch it and display in
my webpage using PHP.
I am not sure what you mean by "directly storing in the database" .. did you mean entering data using PhpMyAdmin or any other similar tool? If yes, I have tried using PhpMyAdmin to input unicode data, so it has worked fine for me - You could try inputting data using phpmyadmin and retrieve it using a php script to confirm. If you need to submit data via a Php script just set the NAMES and CHARACTER SET when you create mysql connection, before execute insert queries, and when you select data. Have a look at the above posts to find the syntax. Hope it helps.
** UPDATE **
Just fixed some typos etc
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');
mysql_select_db('onlinetest',$con);
$nith = "CREATE TABLE IF NOT EXISTS `TAMIL` (
`data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
if (!mysql_query($nith,$con))
{
die('Error: ' . mysql_error());
}
$nithi = "INSERT INTO `TAMIL` VALUES ('இந்தியா நாட்டின் பக்கங்கள்')";
if (!mysql_query($nithi,$con))
{
die('Error: ' . mysql_error());
}
$result = mysql_query("SET NAMES utf8");//the main trick
$cmd = "select * from TAMIL";
$result = mysql_query($cmd);
while($myrow = mysql_fetch_row($result))
{
echo ($myrow[0]);
}
?>
</body>
</html>
For those who are looking for PHP ( >5.3.5 ) PDO statement, we can set charset as per below:
$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
CREATE DATABASE hindi_test
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
USE hindi_test;
CREATE TABLE `hindi` (`data` varchar(200) COLLATE utf8_unicode_ci NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `hindi` (`data`) VALUES('कंप्यूटर');
For Those who are facing difficulty just got to php admin and change collation to utf8_general_ci
Select Table go to Operations>> table options>> collations should be there

Categories