json_encode, utf8 and Greek characters - php

I am writing an android app and i want to query a db and get the results with json_encode.
So i have this db:
CREATE DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
I use utf8 because i want to store greek characters. All the tables have utf8_general_ci collation.
At last i have this php service that i use to query my db.
<?php
$host = "localhost";
$username = "root";
$password = "password";
$dbname = "my_db";
$con = mysql_connect($host,$username,$password) or die ('Error connecting to mysql');
mysql_query("SET character_set_results=utf8", $con);
mysql_query("SET NAMES 'utf8'", $con);
mysql_select_db($dbname,$con);
$query = "SELECT * from patients";
$result = mysql_query($query,$con);
while($e=mysql_fetch_assoc($result))
$output[]=$e;
print(json_encode($output));
mysql_close($con);
?>
The problem is that in the output i dont get greek characters. Instead i get this:
[{"patient_id":"2","email":"patienta#mail.com","password":"password2","firstname":"\u03a7\u03a1\u0397\u03a3\u03a4\u039f\u03a3","lastname":"\u039c\u03a0\u0391\u0396\u0399\u03a9\u03a4\u0397\u03a3","birth":"1989-11-11","mobile":"6941212121","tameio":"\u039f\u0393\u0391"}]

That is right. All the chars not in ascii will be converted into UTF-8 format.
The data will be decoded correctly by json_decode.

Related

fixing database encoding to UTF8

I need to change encoding of whole database to UTF8.
I was connect to database like this before:
// MySQL connect information.
$username = "root";
$password = "";
$host = "localhost";
$database = "yac";
// Connect.
$connection = mysql_connect($host, $username, $password)
or die ("It seems this site's database isn't responding.");
mysql_select_db($database)
or
die ("It seems this site's database isn't responding.");
my database content was stored to database without mysql_query("SET NAMES UTF8"); and data was like this:
باشگاه هوانوردان جوان
now recently I connect to database like this:
$mysql_username = "root";
$mysql_password = "";
$mysql_host = "localhost";
$mysql_database = "tick5";
$connection = mysql_connect($mysql_host, $mysql_username, $mysql_password) or die ("It seems this site's database isn't responding.");
mysql_select_db($mysql_database) or die ("It seems this site's database isn't responding.");
mysql_query("SET NAMES utf8");
mysql_query("SET CHARACTER_SET utf8");
and stored data is like this:
عنوان مقاله
since I use mysql_query("SET NAMES UTF8"); before INSERT queries I haven't any problem but now I need my old content and I want to change encoding and fix this.
How to do this?
NOTE : my tables Collation is utf8_unicode_ci
Try to see if this can help you. http://www.gentoo-wiki.info/TIP_Convert_latin1_to_UTF-8_in_MySQL
If you do not have a running Linux system, you can download an Ubuntu LiveCD and run it directly from the CD. http://www.ubuntu.com/download/desktop

change mysql table character set

I have a table that data was inserted in it some time ago. data included by php and character set in it was utf-8 .
mysql_query("set names utf8");
now ,I need this table in another project which all data will show on php mysql default character set.
problem : my data is persian, when I set charachter set utf-8, every things is ok, but without character set data convert to the "?????" what should I do?!
I want to import all data form old table to new table on new character set !!
Try this might help you
<?php
header('Content-Type: text/html; charset=utf-8');
?>
and then in the connection
<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>
///
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
mysql_query("set names utf8");
$sql = "SELECT * FROM `region` ";
$res = mysql_query($sql);
while($h=(mysql_fetch_array($res)))
{
$row[] = $h;
}
mysql_close($db);
/// set another character set
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
foreach ($row as $item)
{
echo $sql = "insert into `region2` (id,name,myId) values (".$item['id'].",'".$item['name']."','".$item['myId']."') ";
mysql_query($sql);
}
#afsane, using mysqldump, you can dump the data from old_table and then import it into the new_table which has the new character encoding.
mysqldump -u user -p --no-create-info schema old_table > old_table.dump
mysql -u user -p schema new_table < old_table.dump
This will be much faster than doing a conversion via PHP.

Special characters are stored as ? in my mysql DB

I use the following code to store and update data in my db.
<?php
header("Content-Type: text/html;charset=UTF-8", true);
mysql_connect("localhost", "test", "test") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
// mysql_set_charset('UTF8');
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
mysql_query("UPDATE `test`.`english` SET age = '".$age."', city = '".mysql_real_escape_string($_REQUEST['city'])."', aboutMe = '".$_REQUEST['about']."' WHERE `english`.`username` = '".$_REQUEST['username']."' LIMIT 1 ;")
or die(mysql_error());
The code is working well. But if the string, e.g. "city" contains a ä,ö,ü it is stored as ?
So for example the word "Bär" goes to "B?r".
I have already added these utf8 things but it does not help.
The server is only supporting php4.x
Edit: The DB is using latin1_general_ci as collation.
Use utf8 as collation, maybe utf8_bin or utf8_unicode_ci
(The ci means it's case insensitive so comparsions like "John" = "JOHN" return true). You can do this by running the following query in MySQL:
alter table `test`.`english` convert to character set utf8 collate utf8_unicode_ci;
change the table collation to utf8_bin.
To change the default character set and collation of a table including those of existing columns (note the convert to clause):
ALTER TABLE tableName CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin
in utf8_general_ci
ä = a
ö = o
ü = u
but in utf8_bin
ä != a
ö != o
ü != u
Keep ut8_unicode_ci and add this to your php code:
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_query("set names 'utf8'",$connection);
I am using this everywhere (Cyrillic, Chinese etc ) and it is working for every query (insert or update).
<?php
header("Content-Type: text/html;charset=UTF-8", true);
$connection = mysql_connect("localhost", "test", "test") or die(mysql_error());
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_query("set names 'utf8'",$connection);
mysql_select_db("test") or die(mysql_error());
mysql_query("UPDATE `test`.`english` SET age = '".$age."', city = '".mysql_real_escape_string($_REQUEST['city'])."', aboutMe = '".$_REQUEST['about']."' WHERE `english`.`username` = '".$_REQUEST['username']."' LIMIT 1 ;")
or die(mysql_error());
I got it working. I have no explanation for this behavior.
I have changed in the php file from utf8 to latin1:
mysql_query("SET NAMES 'latin1'");
mysql_query("SET CHARACTER SET latin1");
mysql_query("SET COLLATION_CONNECTION = 'latin1_general_ci'");
In the MySQL DB the collation is utf8_unicode_ci for the table and column.
It is working now for all characters beside the € symbol.

Saving Japanese characters with PHP

I'm currently trying to save a japanese-character string with MySQL in PHP. The characters are saved as questionmarks. This is my query:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$result = mysql_query("INSERT INTO battles (basic_words) VALUES ('".mysql_real_escape_string($basic_words)."'");
The string "$basic_words" definitely contains japanese characters but they are not saved. The coalition for the row "basic_words" is utf8_general_ci
php mysql query encoding problem suggests
$db_con= mysql_connect('localhost', 'user', 'password');
if( function_exists('mysql_set_charset') ){
mysql_set_charset('utf8', $db_con);
}else{
mysql_query("SET NAMES 'utf8'", $db_con);
}
Also Check http://php.net/manual/en/function.mysql-set-charset.php

How can i store Hindi data in xampp PHPmyadmin?

How can i store Hindi data in Mysql?
I want to code using zend.
I created 1 table in Mysql, I set collation as utf8_general_ci.
You should probably use UTF-16 rather than UTF-8. See: http://forums.mysql.com/read.php?103,86499,86910
As you are coding using ZEND PHP Framework, just before the SQL Query for insert/select add the 2 lines for Setting NAMES & Character Set:
$dbhost = 'xx.xx.xx.xx';
$dbuser = 'xyz';
$dbpass = 'xxxxxx';
$dbname = 'yyyy';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
mysql_query("SET NAMES utf8");
mysql_query("set characer set utf8");
$result = mysql_query("SELECT * FROM tablename etc...");

Categories