Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use other languages in PHP echo or print

Tags:

php

mysqli

I created account in hostinger.in and created a php that will fetch contents from database table. People saying my question is duplicate of this question.

Things i tried from this question which you are saying as duplicate

What they provided there is to Specify the utf8mb4 character set on all tables and text columns in your database . But my problem is different here. I already specified my encoding in TABLE. But then also while printing in php, it is not showing the result properly.

Another thing what they provided is to use below code

$mysqli->set_charset('utf8mb4');       // object oriented style
mysqli_set_charset($link, 'utf8mb4');  

I used that too, but i didn't got output.


This is my db.php

<?php

$con=mysqli_connect("HOST","mysql","mypass","u558167714_mydb");

if(!$con)
{
echo "Connection error... ".mysqli_connect_error();
}
else
{
echo "Connection Successful..."
}
?>

And my index.php

<?php
require_once 'db.php';
$query =  "SELECT * FROM quotes LIMIT 1";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result)){
print(json_encode($row['quote']));
}

?>

The actual content in my database is This is தமிழ்

But what i am receiving when i am getting is "This is \u0ba4\u0bae\u0bbf\u0bb4\u0bcd " .

This is my url.

I saw lots of questions similar to mine and tried following commands,

mysqli_set_charset($this->mysqli,"utf8"); or
mysqli_set_charset($conn,"utf8");

but i am not getting the correct output.

How can i actually set my character set to get my output.

like image 403
Ganesh Avatar asked Dec 14 '22 09:12

Ganesh


2 Answers

Why are you even calling json_encode()? The database clearly has a valid string in it, then you're encoding that into JSON, which yes... will turn non ascii characters into \uXXXX escape sequences. That's something you'd do if your consumer were Javascript code (or something else which speaks JSON). You're just viewing this in the browser, so you should output it as HTML (which plain text happens to be a subset of).

Just do: print $row['quote'];

Or better yet, so that you get used to being safe:

print htmlentities($row['quote'], ENT_QUOTES, 'UTF-8');
like image 146
Sara Avatar answered Dec 25 '22 10:12

Sara


Test with and without the database call.

<?php

$data = array(
    'name' => 'தமிழ'
    );

header('Content-Type: application/json; charset=UTF-8'); 
print json_encode($data, JSON_UNESCAPED_UNICODE);
like image 26
Progrock Avatar answered Dec 25 '22 11:12

Progrock