Display JSON data in Marathi language using PHP web service - php

I have a PHP web service in which I am retrieving data from mysql database. In database the data is stored in Marathi language, but when I am running my web service in postman, data is coming but many question mark symbol are displayed. How to solve this issue? Below is my PHP web service code:
<?php
//include config_db.php
include_once('config_db.php');
$qr = "SELECT * FROM `recipe_breakfast`";
$qrResult = mysqli_query($db, $qr);
$count = mysqli_num_rows($qrResult);
if($count > 0)
{
while($data = mysqli_fetch_assoc($qrResult))
{
$result[] = $data;
}
$response = array('msg' => 'Success','details' => $result);
}
else
{
$response = array('msg' => 'No details found');
}
#mysqli_close($db);
/* Output header */
header('Content-type: application/json');
echo json_encode($response);
?>

You have a charset issue.
In your code, after this line:
include_once('config_db.php');
Add this:
mysqli_query($db, 'set names utf8mb4');
If somehow your database doesn't support utf8mb4, use this instead:
mysqli_query($db, 'set names utf8');
This should solve the problem.
If it doesn't, you also have an issue in your table.
The solution is to set the collation of every varchar or text column to
utf8mb4_general_ci.
This means that you have to redo the data entry.

Related

symfony inserting data using doctrine and retrieving it using php

I'm working on a symfony2 project. when i insert data with french accents like (é), it is inserted as shown in this image:
when i try to retrieve this data using a php file to use it in android app, i use this code:
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','adproject');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect to db');
$sql = "select * from actualite";
$result = array();
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'login'=>$row[1],
'password'=>$row[2]
));
}
echo json_encode($result);
mysqli_close($con);
?>
I get nothing because of the accents in the database. Any suggestion?
I suspect you'll need to go through this Configuring the Database section and setting up for UTF8:
http://symfony.com/doc/current/doctrine.html#configuring-the-database
Let us know if you've already done that.

PHP json_encode is returning local language characters as html entities. Output needed in unicode

My php web service uses json_encode to return json data fetched from MySQL database table. The data should be returned in whatever local language as unicode. Instead local characters like hindi/telugu/bengali are displayed as html entities. But output is needed as unicode and NOT html entities.
<?php
//header('Content-Type: text/html;charset: utf8'); //wasted code line
//Connection and credentials
$servername = "xxx.yyy.zzz.nnn";
$username = "username";
$password = "password";
$dbname = "mydb";
//variables to store fetched data
$item[]=array();
$dataArray[] = array();
// Create connection
$conn = mysql_connect($servername, $username, $password);
//mysql_set_charset('utf8', $conn); //wasted code line
$mytopid = mysql_real_escape_string($_GET['mytopid']); //get input
$query = "SELECT * FROM datamaster where Id > '$mytopid' order by Id desc"; //Now query
//mysql_query("set names 'utf8'"); //wasted code line
if ($result=mysql_query($query,$conn)){
$rows = mysql_numrows($result);
$i= 0;
while ($i < $rows){
//fetch data
$row = mysql_fetch_array($result);
//wasted code lines
//$dataArray[$i]["shortid"] = utf8_encode($row['Id']);
//$dataArray[$i]["shorttitle"] = utf8_encode($row['Title']);
//reality
$dataArray[$i]["shortid"] = $row['Id'];
$dataArray[$i]["shorttitle"] = $row['Title'];
$i++;
}
$item[0]["response"] = 1;
$item[1]["matching"] = $rows;
$item[2]["events"]=$dataArray;
echo json_encode($item);
}else{
$item[0]["response"] = 0;
}
//echo json_encode($item, JSON_HEX_TAG| JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); //wasted code line
mysql_close($conn);
?>
Got Output:
Actual Output
Required Output:
[{"shortid":"5","shorttitle":"\u0c38\u0c32\u0c4d\u0c2e\u0c3e\u0c28\u0c4d\u200c \u0c05\u0c21\u0c3f\u0c17\u0c3f\u0c28\u0c3e \u0c05\u0c35\u0c15\u0c3e\u0c36\u0c2e\u0c3f\u0c35\u0c4d\u0c35\u0c32\u0c47\u0c26\u0c41!"}]
I finally convert it at my client program decode to local language.
The required output is supposed to be the default behaviour of json_encode. Despite most trials per php documentation (see the commented CODE lines that show my trials //wasted code line) the output continues to be in html entities except for English language.
My client programming language does not translate html entities.
Is there a php way to get the required output?
I have tried every possible concept on stack overflow and in php documentation. Now I need help.
Please refer to my database language settings
Use json_encode($item, JSON_UNESCAPED_UNICODE);. It will encode multibyte characters literally.
You can get more info here.
Actually it's strange your client side cannot handle escaped characters. What do you use exactly?
Thnak you Wazelin for the prompt reply. I had tried the JSON_UNESCAPED_UNICODE approach too earlier. But that output continued to be html entities.
A temporary fix to the problem I have incidentally tumbled in to using html_entity_decode.
I changed the code lines in while loop to:
while ($i < $rows){
//fetch data
$row = mysql_fetch_array($result);
//reality
$dataArray[$i]["shortid"] = $row['Id'];
//handle html entities of local language
$dataArray[$i]["shorttitle"] = html_entity_decode($row['Title']);
$i++;
}
That had given the required output.
But doing this to every single web service is not like an ideal solution.
Quite strangely the default json_encode behaviour is very elusive in my case.
Check documentation:
http://php.net/manual/en/function.json-encode.php
There are some options which allows you to encode your data correctly.

Code does not print arabic text from database

The code below that I am using will not print text that is of arabic form from the database although the collation of the table is uft8_general_ci and the database is of collation uft8_general_ci.
code:
<?php
// Create connection
$con = mysqli_connect("localhost", "", "", "");
mysqli_set_charset('utf8', $con);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Mothakirat";
mysqli_set_charset('utf8', $sql);
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while ($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo htmlentities(json_encode($resultArray));
}
// Close connections
mysqli_close($con);
?>​
How can I get the arabic to print properly?
When working with non-Latin characters in a web based environment, I find there are three points of failure:
1) You need to tell mysql what output to give you when you connect (which you've already done, I think):
mysqli_query('SET CHARACTER SET utf8');
2) You probably need to add
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
into the <head> section in your html.
3) You need to ensure that the font used to display the text has glyphs for the characters you are expecting to see (Times New Roman may not suffice). I have done stuff with Greek and Hebrew and I often find fonts have basic support but not when there are diacritics.
May be a long shot but have you checked the charset of the actual field?
https://dev.mysql.com/doc/refman/5.0/en/charset-column.html

wrong '.$row["column_name_here"].' encoding

I'm a bit newbi in PHP. I implemented http://www.sanwebe.com/2013/03/loading-more-results-from-database solution in my new website: http://do2go.pl/do2go-nowa/
its working well - except encoding. My DB and all things are in UTF8. When fetch taking data from DB UTF8 seems not working.
Heres configuration and fetch code:
Config:
<?php
$db_username = 'kamio2_do2gonowa';
$db_password = 'JeremiasZ1!';
$db_name = 'kamio2_do2gonowa';
$db_host = 'localhost';
$item_per_page = 2;
mysqli_query ("SET NAMES 'utf8'"); mysqli_set_charset('utf8');
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name) or die('could not connect to database');
?>
And fetch:
<?php
include("config.inc.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
header('Content-Type: text/html; charset=UTF-8');
//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($connecDB, "SELECT id,introtext FROM w7wst_content ORDER BY id DESC LIMIT $position, $item_per_page");
//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo '<li id="item_'.$row["id"].'"><span class="page_message">'.$row["introtext"].'</span></li>';
}
echo '</ul>';
?>
I added mysql_query ("SET NAMES 'utf8'"); mysql_set_charset('utf8'); but this does nothing. Still getting � symbols and ? instead of " All next 3 was changed directly in DB tables for html symbols which isn't a solution.
Any help appreciated! :)
(To close the question, since this was the solution to the OP's problem)
Add $connecDB->set_charset("utf8"); just before $results = mysqli_query($connecDB...
You are using mysql and mysqli in one script. Only use mysqli as mysql has been deprecated. That said try this:
mysqli_character_set_name($connectDB);
mysqli_set_char_set($connectDB, 'UTF8');
Set these after your connection script.
Did you check if they are properly stored in the table? Try typing this in your database(not in PHP):
SELECT * FROM w7wst_content;
and see if it's displaying properly. Alternatively you can check table contents with some tool like phpmyadmin. If it's not you have to set collation to something like utf8_general_ci.
EDIT: Also, how about setting the charset AFTER connecting to the database, hm? :)

Query fail with no error

I have a database UTF8 encoded, because it contains arabic letters. I have a php file to select all values from a given table:
<?php header('Content-type: application/json; charset=utf-8');
$host = "localhost"; //Your database host server
$db = "kalimat"; //Your database name
$user = "root"; //Your database user
$pass = ""; //Your password
$connection = mysqli_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
die(mysqli_error($db));
}
else
{
//Attempt to select the database
$dbconnect = mysqli_select_db($connection, $db);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$auteur=utf8_decode(urldecode($_GET["artist"]));
//$auteur=utf8_decode($auteur);
$resultset = mysqli_query($connection,"SELECT * FROM $auteur");
if( $resultset )
{
$records = array();
//Loop through all our records and add them to our array
while($r = mysqli_fetch_assoc($resultset))
{
$records[] = $r;
}
$records = array_map(function($r) {
$r['artist'] = utf8_encode($r['artist']);
return $r;
}, $records);
//Output the data as JSON
echo json_encode($records);
}
else{
die("Error:".mysqli_error()); //Here is the error
}
}
}
?>
In output, I have "Error:" without any specification, so I can't know the source of the problem.
Any something strange in the php file?
Thank you for your help.
have you tried utf8mb4 ?
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
utf8mb4 is superset of utf8
Advice: database ,table names and columns you should make them in non utf8 characters.
EDIT:
to handle with utf8 characters you have to make all in utf8 such as:
your files , convert them to utf8 , all pages where you are using utf8 also that page where you geting the author fom.
database , tables , columns.
your editor where you write codes.
headers.
...
try putting the following lines just after the php tag starts and error will be displayed.
error_reporting(E_ERROR);
ini_set("display_errors",1);
hope this helps.
You are using mysqli, not mysql. So you have to use appropriate error function, and call it according to it's proper syntax.
Also, your idea on storing data in user-defined tables is wrong. You have to learn relational database basics first.

Categories