How to Fetch Data in Hindi and Gujarati Language in MySQL? - php

Below my code i try this solution but not work it. and also i give utf8_unicode_ci for hindi and utf8_bin for Gujarati Language in database Collation.
Please Help me how i fetch data Hindi & Gujarati Language.
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<?php
session_start();
require_once('config.php');
$JSONoutput = array();
$q=mysqli_query($con,"SELECT * FROM tbl_Hindi");
header('Content-Type: text/html; charset=UTF-8');
while($rs=mysqli_fetch_assoc($q))
{
$JSONoutput['SMS'][] = array("ID"=>$rs['ID'],"Message"=>$rs['Message']);
}
print(json_encode($JSONoutput));
?>
Output:
{"SMS":[{"ID":"1","Message":"?? ????? ?? ??? ???? ??, ???? ?? ???? ??? ?? ????? ?? ?? ???? ?? ??????"},{"ID":"2","Message":"???? ????? : ??? ???? ?? ? ????? ????? : shopping ???? ?? ??? ???? ????? : : ???? ?? ???? ? ????? ????? : ???? ??? ??? ?? Gf ?? ? ???? ????? : ?? ?? ??? ??? ?? ? ????? ????? : ?? ???? "}]}

Your sql table change field format change language collection
utf16_general_ci all language accept.
ALTER TABLE tbl_Hindi CHANGE Message Message VARCHAR( 50 ) CHARACTER SET utf16 COLLATE utf16_general_ci NOT NULL ;
Try this query

See https://stackoverflow.com/a/38363567/1766831 -- In particular the discussion of "Question marks". It says
The bytes to be stored are not encoded as utf8/utf8mb4. Fix this.
The column in the database is CHARACTER SET utf8 (or utf8mb4). Fix this.
Also, check that the connection during reading is UTF-8.
The question marks are already in the database table; the original data is lost.
Do not us ucs2 or utf16; use only utf8 or utf8mb4.

Related

Return JSON Data in regional Language in PHP

I am using a MYSQL database where in I store the data in collation : utf16le_general_ci. I did this to support my regional language in the database and I am able store the data in my regional language(GUJARATI) successfully.
But when I use PHP(version 7+) to read the data and echo it in JSON format I get this kind of result :
[
{
"content_id":"1",
"cat_id_FK":"1",
"content_data":"??????????? ? ????? ??????? ??."
},
{
"content_id":"2",
"cat_id_FK":"1",
"content_data":"???? ?? ??? ?? 80% ????? ???? ??? ? ??? ??, ???? ??? ?????? ?? ??????,?????????? ??? ?????\r\n??????? ?? ??? ??????? ???? ??? ??? ??."
},
{
"content_id":"3",
"cat_id_FK":"1",
"content_data":"?????????? ??????? ????????? ???? ???? ???? ? ???????????."
},
{
"content_id":"4",
"cat_id_FK":"1",
"content_data":"??????????? ??? ???? ?????? ?????? ???? ???? ????? ?? ???????? ?? ??? ??."
},
{
"content_id":"5",
"cat_id_FK":"1",
"content_data":"??? ????? ????? ???????? ?? ????? ????? ??? ??????? ? ??????????? ?? ????? ???? ????."
},
{
"content_id":"6",
"cat_id_FK":"1",
"content_data":"??????????? ???? ?? ??? ??? ??? ? ????? ???."
}
]
My Code :-
header('Content-Type:application/json;charset=utf-8');
//header('Content-Type:application/json;charset=utf16le_general_ci');
include 'init.php';
global $connect;
$query = "SELECT * FROM gsrahasyacontent WHERE cat_id_FK=1";
$queryResult = mysqli_query($connect, $query);
while ($row = mysqli_fetch_assoc($queryResult)) {
$array[] = $row;
}
echo json_encode($array, JSON_UNESCAPED_UNICODE);
//print json_encode($array, JSON_UNESCAPED_UNICODE);
-> I tried changing out the charset to utf16le_general_ci from utf-8 but that didn't worked.
-> I also used print instead of json but still got the same result.
This is what my table looks like.
try this mysqli_set_charset($con,"utf8");

Show lingual text kept in varchar (utf8_unicode_ci) in MySQL server in Android app with JSON php script

I have gone through many posts on the same topic but still not able to figure out what is the solution. If you think that an available solution will work for me then you can direct me there too.
Now, I want to display text in my regional language (Hindi) inside my app. This text is stored in a table on my website server MySQL database. I am retrieving this data along with many more columns through an SQL query run through a php script in the form of a JSON.
When I am testing this JSON on JSONLint and also by taking a log of the json string received by the Async task inside the app, all the fields having Varchar type and 'utf8_unicode_ci' collation are showing as below with question marks. Other fields types are coming correctly.
"?????? ??? ???? ?? ??????? ?? ????? ???? ?? 10 ?????"
Server details
phpMyAdmin Version 4.7.3
PHP version: 5.6.30
Database
Server type: MySQL
Server version: 5.6.38 - MySQL Community Server (GPL)
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)
Below is my table in the database. The difference in utf8 in the image is because I was trying if utf8mb4 works.
The database is MyISAM type and thus there is no 'nvarchar' field type.
Below is my php script. I have two tables, Table 1 contains non-localized fields, Table 2 has the localized translation fields. 'hi' is my language code. The above table image is of the TableTranslation.
<?php
$host='localhost';
$hostname='*******';
$password='*******';
$db='*******';
$sql="SELECT TablePost.PostID,TableTranslation.PostCategory,TablePost.PostImage,TableTranslation.PostTitle,TableTranslation.PostBrief,
TableTranslation.PostLink,TablePost.IsShort,TablePost.IsSeenBy
FROM TablePost LEFT JOIN TableTranslation ON TablePost.PostID = TableTranslation.PostID
WHERE TableTranslation.Language LIKE 'hi';";
$con=mysqli_connect($host,$hostname,$password,$db);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result)){
array_push($response, array(
"PostID" => $row[0],
"PostCategory" => $row[1],
"PostImage" => $row[2],
"PostTitle" => $row[3],
"PostBrief" => $row[4],
"PostLink" => $row[5],
"IsShort" => $row[6],
"IsSeenBy" => $row[7]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($con);
?>
Below is the JSON response I am getting.
{
"server_response": [{
"PostID": "1",
"PostCategory": "??????????",
"PostImage": "http:\/\/websitename.com\/image.png",
"PostTitle": "?????? ??? ???? ?? ??????? ?? ????? ???? ?? 10 ?????",
"PostBrief": "???? ?? ????????? ?? ???-??? ?????? ?? ??? ???????? ???? ???? ?? ?? ??? ????? ??? ??? ?????? ????? ???? ???? ??? ?? ?? ???? ?? ??????? ?? ????? ???? ?? ??? ???????? ????? ???? ????",
"PostLink": "http:\/\/websitename.com\/post_link\/",
"IsShort": "0",
"IsSeenBy": "98"
}]
}
How do I correct it? Will really appreciate any help here.
add line mysqli_set_charset( $con, 'utf8'); after mysqli_connect()
like below code
<?php
$host='localhost';
$hostname='*******';
$password='*******';
$db='*******';
$sql="SELECT TablePost.PostID,TableTranslation.PostCategory,TablePost.PostImage,TableTranslation.PostTitle,TableTranslation.PostBrief,
TableTranslation.PostLink,TablePost.IsShort,TablePost.IsSeenBy
FROM TablePost LEFT JOIN TableTranslation ON TablePost.PostID = TableTranslation.PostID
WHERE TableTranslation.Language LIKE 'hi';";
$con=mysqli_connect($host,$hostname,$password,$db);
mysqli_set_charset( $con, 'utf8'); //change here.
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result)){
array_push($response, array(
"PostID" => $row[0],
"PostCategory" => $row[1],
"PostImage" => $row[2],
"PostTitle" => $row[3],
"PostBrief" => $row[4],
"PostLink" => $row[5],
"IsShort" => $row[6],
"IsSeenBy" => $row[7]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($con);
?>
also try to add collation utf8_general_ci to PostTitle and PostBrief
like
I had a similar issue with Arabic and Hindi i solved like below.You can solve this issue in MySQL side.
In the table structure, you have to change collation utf8_bin
"PostID" - `utf8_bin` instead of `latin`,
"PostCategory" - `utf8_bin` and so on.

Spreadsheet_Excel_Reader one column show question mark reading in PHP

Please check this image ###### value showing que mark. If I upload this column value on database this value save with ??????? like.
I am using Spreadsheet_Excel_Reader in php
$data = new Spreadsheet_Excel_Reader("/var/www/office/office/public_html/aumfiles1/$file",true);
$folioNumber = addslashes(trim($data->val($j,3)));
??????????
SELECT rUnits FROM test_trans1 WHERE folioNumber='??????????' and productCode='test' and arn_id='66' and data_type='KARVY'
update test_trans1 set rUnits = '1458.747' WHERE folioNumber='??????????' and productCode='test' and arn_id='66' and data_type='KARVY'
??????????

PHP MySQL utf-8 Euro symbol shown as questionmark on a diamond

So after a whole day of googling and debugging I end up here.
MySQL
set to the following encoding:
db: utf8_general_ci
table: utf8_general_ci
column: utf8_general_ci, TEXT
I put in some euro symbols and some other weird characters
acentuação €€€€€
PHP (codeigniter)
config
$config['charset'] = 'UTF-8';
dsn
char_set=utf8,dbcollat=utf8_general_ci
I made some queries to compare
model
$query = $this->db->query("SET NAMES latin1");
$query = $this->db->query("SELECT shortdesc,HEX(shortdesc) FROM `contracttypes` WHERE id = 4");
$ret['latin1'] = $query->row();
$query = $this->db->query("SET NAMES utf8");
$query = $this->db->query("SELECT shortdesc,HEX(shortdesc) FROM `contracttypes` WHERE id = 4");
$ret['utf8'] = $query->row();
return $ret;;
controller
public function utfhell() {
var_dump($this->campagne_model->utfhell());
}
This outputs
array (size=2)
'latin1' =>
object(stdClass)[34]
public 'shortdesc' => string 'acentua��o �����' (length=16)
public 'HEX(shortdesc)' => string '6163656E747561C3A7C3A36F20E282ACE282ACE282ACE282ACE282AC' (length=56)
'utf8' =>
object(stdClass)[33]
public 'shortdesc' => string 'acentuação €€€€€' (length=28)
public 'HEX(shortdesc)' => string '6163656E747561C3A7C3A36F20E282ACE282ACE282ACE282ACE282AC' (length=56)
So far so good, on to a
view
<?php header('Content-Type: text/html; charset="utf-8"', true); ?>
<!doctype html>
<html>
<head>
<title>UTFhell</title>
<link rel="stylesheet" href="../assets/css/style.css"/>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
...
<?php
echo 'Original : ', $campagne_info->contractName->shortdesc."<br />";
echo 'UTF8 Encode : ', utf8_encode($campagne_info->contractName->shortdesc)."<br />";
echo 'UTF8 Decode : ', utf8_decode($campagne_info->contractName->shortdesc)."<br />";
echo 'TRANSLIT : ', iconv("ISO-8859-1", "UTF-8//TRANSLIT", $campagne_info->contractName->shortdesc)."<br />";
echo 'IGNORE TRANSLIT : ', iconv("ISO-8859-1", "UTF-8//IGNORE//TRANSLIT", $campagne_info->contractName->shortdesc)."<br />";
echo 'IGNORE : ', iconv("ISO-8859-1", "UTF-8//IGNORE", $campagne_info->contractName->shortdesc)."<br />";
echo 'Plain: ', iconv("ISO-8859-1", "UTF-8", $campagne_info->contractName->shortdesc)."<br />";
echo '€€€€€€€€€€<br>';
?>
None of these now show me a normal euro symbol except the final echo statement, they all give me questionmark diamonds for the eurosymbols
The HEX is the utf8 encoding for that string. So the data is in the table 'correctly'.
The black diamond (�) is the browser's way of saying wtf. It comes from having latin1 characters, but telling the browser
to display utf8 characters.
You could tell the browser to display "Western", that is avoiding the underlying problems.
Remember, the goal is to really use utf8.
Sometimes this occurs together with Question Marks, in which case you must start over.
The cause (probably):
The bytes you had were encoded latin1. You acquired them from somewhere -- file dump online input, etc.
The connection parameters said latin1.
The column/table is declared to be CHARACTER SET said utf8, so during INSERT, they were correctly converted.
When SELECTing, the seting in step 2 was again latin1, so they were converted back to latin1.
When displaying text in a web page, the page's header said that the bytes were utf8.
Solution, Plan A: (Sloppy, but probably workable)
Change #5 so say the appropriate equivalent of latin1.
Solution, Plan B:
Fix the source to be utf8-encoded
query("SET NAMES utf8") (unless there is a way to set it at connect time)
Leave the table/column at CHARACTER SET utf8
Step 2 cover this.
Leave <meta ... UTF-*>.

json output with native languages?

i am using json output for my application and stored all data in my native language in mysql server with utf8_general_ci
when i am fetching that using json_encode i got the json array but the data format is not supported in that. how can i solve it.
code which i used to create json data.
<?php
header('Content-type: text/html; charset=utf-8; pageEncoding="ISO-8859-1"');
include('include/config.php');
mysql_query("SET NAMES 'utf-8' 'ISO-8859-1'");
//mysql_query("SET CHARACTER SET utf8 ISO-8859-1");
$sth = mysql_query("select v.verse,b.book_name,v.chapter,v.verse_number from tbl_verses_mal v inner join tbl_books_mal b on v.book_id=b.book_id");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
?>
the output i got is like
[{"verse":"???????? ?????? ???????? ?????? ??? ????????? . ?? ????????? ??? ??????? ????????????????? .","book_name":"Genesis","chapter":"1","verse_number":"1"},{"verse":"???? ??????? ?????????? ??????? : ???????????? ???? ????????????? .????????????? ??????? ??????","book_name":"Genesis","chapter":"1","verse_number":"2"},{"verse":"???????? ?????????? ????? ???? ?????????: ???????? ??????? ","book_name":"Genesis","chapter":"1","verse_number":"3"}]
????? marks represents the language which is in the database.
the expected results is like given below
[{"verse":"അയൽകാരന് ആവശ്യം വരുമ്പോൾ നിങ്ങൾ കടം കൊടുക്കു. .","book_name":"Genesis","chapter":"1","verse_number":"1"},{"verse":"ഭൂമി പാഴായും ശൂന്യമായും ഇരുന്നു : ആഴത്തിന്മീതെ","book_name":"Genesis","chapter":"1","verse_number":"2"},]
how can i solve this issue??
mysql_query("SET NAMES 'utf-8' 'ISO-8859-1'");
This makes no sense. Set the charset properly:
mysql_set_charset('utf8');
you need to set character set properly try like
mysql_set_charset('utf8');
And mysql_* function are deprecated use PDO or Mysqli instead

Categories