4

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.

Community
  • 1
  • 1
Ganesh
  • 1,820
  • 2
  • 20
  • 40
  • Just add `` in webpage – Somnath Muluk Dec 17 '15 at 16:38
  • Man... I am not using any HTML, where do you want me to add it in above two files? – Ganesh Dec 17 '15 at 16:58
  • You can use header function as mentioned in following answer or echo the meta tag which is also mentioned in following answer. – Somnath Muluk Dec 17 '15 at 17:00
  • I got the error when I tried with echo – Ganesh Dec 17 '15 at 17:01
  • @SomnathMuluk When i used **echo "";** , this error came **Parse error: syntax error, unexpected 'Content' (T_STRING), expecting ',' or ';' in /home/u242267766/public_html/kbt.php on line 3** – Ganesh Dec 17 '15 at 17:05
  • Use `echo ''` – Somnath Muluk Dec 17 '15 at 17:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98260/discussion-between-ganesh-and-somnath-muluk). – Ganesh Dec 17 '15 at 17:11
  • In HTML5 all you need is `` ... Also just take a look at what you're echoing... if you're surrounding your string in double quotes, you need to escape the double quotes within the string itself. Like `echo ""` – Typel Dec 17 '15 at 17:28
  • @Typeless '' - Is this correct code, because double quotes starting at "text/html and ending at utf-8 ? – Ganesh Dec 17 '15 at 17:33
  • @Typeless : I used this **echo '';** but got the same output **"This is \u0ba4\u0bae\u0bbf\u0bb4\u0bcd "** No change :( – Ganesh Dec 17 '15 at 17:45
  • Guys, anybody there...?? – Ganesh Dec 17 '15 at 17:59
  • Possible duplicate of [UTF-8 all the way through MySQL, PHP, and HTML](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through-mysql-php-and-html) – Danack Dec 17 '15 at 18:06
  • @Danack : But the answer provided in that link is not giving me the solution. That's why i asked the question. – Ganesh Dec 17 '15 at 18:07
  • @Ganesh You would get an error, because that is also syntactically incorrect. You used single quotes and escaped your double quotes... It's one or the other... http://php.net/manual/en/language.types.string.php – Typel Dec 17 '15 at 18:15
  • @Typeless I even gave without backslash.. then also the same error.. – Ganesh Dec 17 '15 at 18:20
  • You have no space in your meta tag, for one thing. Also, there is no doctype declaration. Read up on very basic HTML5 headers here: http://www.w3schools.com/html/html5_intro.asp – Typel Dec 17 '15 at 18:22
  • when I view source on your page, I see this: `` and then the string... It should be more like this: ` here are the characters` unless you are trying to render a non-HTML document, in which case set your doctype headers in PHP like in the answer provided below. – Typel Dec 17 '15 at 18:24
  • @Typeless Okay can you say what is the exact line i need to put there to set the character type ? – Ganesh Dec 17 '15 at 18:25
  • I mean, can you insert that text in my above index.php code and type as answer here. So that i can copy paste it. Seriously i am confused ya.. Sorry for that. – Ganesh Dec 17 '15 at 18:27
  • 1
    @Ganesh This is not a copy/paste sort of website. The answer is here on this page, it just takes a very tiny bit of effort to put the pieces together. You need to decide which document type you are creating and then read its specifications. If you are creating an HTML5 document, read about how to declare an HTML5 document and then "echo" those tags to your document. I provided the link to those specs above. I also provided the exact code. – Typel Dec 17 '15 at 18:33
  • I am not creating any HTML files.. I am just trying to get the contents exactly so that i can fetch it into my Android Application which i am creating. I wanted that url to show the exact words without messing up. – Ganesh Dec 17 '15 at 18:36
  • Okay, same deal... in that case you probably want to encode your document as JSON. You can set the doctype in PHP: `header('Content-Type: application/json');` ... without that, your json_encode probably doesn't really do anything helpful. – Typel Dec 17 '15 at 18:40
  • 2
    Talk about a help vampire......... People have given you everything you need to do but they can't possibly piece it all together for you. – patricksweeney Dec 17 '15 at 18:45
  • @Typeless Try to understand me . I saw in some tutorial and doing these things. They worked perfectly when i used only English language. When i tried in my languate, these problems came. I promise i don't know what you are asking even.. :( Now what i want is how to specify that encoding to show my language without messing up – Ganesh Dec 17 '15 at 18:45
  • @Ganesh I told you what to do. Have you tried it yet? I'm not sure what else I can do short of travelling to your location and typing it out on your actual keyboard... – Typel Dec 17 '15 at 18:55
  • @Typeless : Believe me... I tried not only what you said but also the following codes, `$mysqli->set_charset('utf8mb4'); // object oriented style mysqli_set_charset($link, 'utf8mb4'); $dbh = new PDO('mysql:charset=utf8mb4'); print htmlentities($row['quote'], ENT_QUOTES, 'UTF-8'); header('Content-Type: application/json; charset=utf-8'); header('Content-Type:text/html; charset=UTF-8');` Nothing helped me... They are giving errors.. or they are giving messed up output... – Ganesh Dec 17 '15 at 19:01
  • That's why i am asking you to add these necessary lines for the encoding in my above code and paste it here... it'll take only 2 minutes for you.. Don't need to travel to my place.. – Ganesh Dec 17 '15 at 19:02
  • @Typeless : Check Progrock answer.. thats what i wanted.. Now i learned about that header thing.. Simply giving a link and asking people to see the syntax and do yourself will not help them. Just try to provide answer if they have no idea about it. We are posting here after doing all these google searches and then only we are asking your help... Understand that first.. Sorry if i said anything wrong.. I just said what i thought. – Ganesh Dec 17 '15 at 19:23
  • @Ganesh One thing to avoid here is asking people for something to "copy/paste". If you aren't learning anything during this process, you'll just be back tomorrow asking the same exact question. This is a teaching/learning site more than anything. – Typel Dec 17 '15 at 19:38
  • @Typeless You just said me to see the syntax and use some tags.. I did not ask you to copy/paste at the starting... I said you i didn't got the output.. Then you said like your answer was correct which is not correct actually.. That's why i asked you to copy/paste the answer. I didn't asked anyone to copy/paste the answer.. I just asked the solution ... – Ganesh Dec 17 '15 at 19:42

4 Answers4

4

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');
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Sara
  • 719
  • 5
  • 8
  • When i just do **print $row['quote'];** , the output is **This is தமிழà¯** – Ganesh Dec 17 '15 at 18:15
  • Same output when i use **print htmlentities($row['quote'], ENT_QUOTES, 'UTF-8');** – Ganesh Dec 17 '15 at 18:16
  • 1
    Then you didn't set your charset to UTF8 like the other answers told you to. Put this at the top: header('Content-type: text/html; charset=utf-8'); – Sara Dec 17 '15 at 18:26
3

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);
Progrock
  • 7,373
  • 1
  • 19
  • 25
3

The encoding is completely correct! Do not try to remove it.

JSON is for Javascript, and Javascript knows how to handle the characters escaped by \x.... If you do:

<script>
var quote = <?php echo json_encode($quote); ?>;
document.write(quote);
</script>

You'll see the decoding is properly done.

You could turn it off using a constant from http://se2.php.net/manual/en/json.constants.php as an option (I think it is JSON_UNESCAPED_UNICODE) - but it is NOT recommended.

If you don't use Javascript and/or HTML, don't use json_encode().

To be super-correct, you should use:

<?php
// code to fetch data...
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • Sorry i am not getting you, i am totally new to php.And i am using only 2 php files here. I don't know where to paste this – Ganesh Dec 17 '15 at 17:19
  • The – Daniel W. Dec 17 '15 at 17:22
  • I used **header('Content-Type: application/json; charset=utf-8');** but getting the same output **"This is \u0ba4\u0bae\u0bbf\u0bb4\u0bcd "** – Ganesh Dec 17 '15 at 17:25
  • @Ganesh Where do you get that output / how do you print it? Again, the string is **correct** having encoded characters. – Daniel W. Dec 18 '15 at 08:34
  • I used `header('Content-Type: application/json; charset=utf-8');` and `print $data` . I didn't used json_ecode($data) – Ganesh Dec 18 '15 at 09:45
2

Perhaps this solves your problem, I tested here:

in HTML:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

in PHP

header('Content-Type:text/html; charset=UTF-8'); 

from link

If give error, try: utf8_encode and utf8_decode.

Eg: echo utf8_encode($string);

UPDATE Eg: index.php file

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?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']));
}

?>
Community
  • 1
  • 1
user2925795
  • 400
  • 10
  • 29
  • I am not using any HTML file here. Just above two files only I am using. – Ganesh Dec 17 '15 at 16:57
  • When i used **echo "";** , this error came **Parse error: syntax error, unexpected 'Content' (T_STRING), expecting ',' or ';' in /home/u242267766/public_html/kbt.php on line 3** – Ganesh Dec 17 '15 at 17:06
  • 1
    It is a JSON response. – Progrock Dec 17 '15 at 17:15
  • @user2925795 No errors , but getting same output **"This is \u0ba4\u0bae\u0bbf\u0bb4\u0bcd "** ): When i used your edited code – Ganesh Dec 17 '15 at 17:17